@types/mongodb package and had problems.Definitions by: in index.d.ts) so they can respond.Since this PR
Collection.insert{,One,Many} are typed with TSchema from Collection<TSchema>
same as the return type of the query methods. This makes it a bit hard to use
when you want mongodb to pick the _id of an inserted document for you:
Version with _id being part of the TSchema:
import { Collection, MongoClient, ObjectId } from 'mongodb'
interface Person {
_id: ObjectId
name: string
age: number
}
MongoClient.connect().then(async client => {
const db = client.db()
const personCollection: Collection<Person> = db.collection('persons');
const writeResult = await personCollection.insertOne({ name: "Foo Bar", age: 30 }) // type error, `_id` missing
const person = await personCollection.findOne({ _id: writeResult.insertedId })
if (person) {
console.log(person._id);
}
})
Version without _id being part of the TSchema:
import { Collection, MongoClient, ObjectId } from 'mongodb'
interface Person {
name: string
age: number
}
MongoClient.connect().then(async client => {
const db = client.db()
const personCollection: Collection<Person> = db.collection('persons');
const writeResult = await personCollection.insertOne({ name: "Foo Bar", age: 30 })
const person = await personCollection.findOne({ _id: writeResult.insertedId })
if (person) {
console.log(person._id); // type error, _id is not defined
}
})
In either case it's not possible to sensibly type collections anymore :(
I think it would make sense to make the _id property optional for the inserts.
I can make a PR I just wanted to ask for feedback first.
Hi @despairblue,
Personally, I declare _id optional in my schemas, e.g.:
interface Person {
_id?: ObjectId;
name: string ;
age: number;
}
This works okay for me, but it could be better since _id is not really optional, it just depends on the use case. Like you said, it's optional for inserts, but to my knowledge it's always included in find* results, whereas other properties may become optional if projection is used.
I think a PR in that direction (with a few tests in mongodb-tests.ts) would be great.
Perhaps we could just use
type TSchemaInsert<TSchema> = Omit<TSchema, '_id'> & { _id?: any }
Most helpful comment
Perhaps we could just use