Did insertOne in Node.js v4 change to not return the inserted doc?

Are the docs here for the Node.js driver really correct - that insertOne() will not return the inserted doc?

What is the recommended way then for inserting and returning the newly created doc?

Maybe you can help me out here @neal - would love to upgrade to v4 as fast as possible :sweat_smile:

Hi @Alex_Bjorlig,

It is new in driver v4 that our driver is compliant with the MongoDB CRUD spec

When you insertOne you now get the insertedId, so that should provide the only “new” part of the document that’s handled by the database.

const myDoc = { a: 1 }
myDoc._id = (await c.insertOne(myDoc)).insertedId
// myDoc is now the same as the doc inserted into mongodb :slight_smile:

3 Likes

Thanks for the answer @Lauren_Schaefer. I did the refactor from v3, not so bad. But probably something that should be mentioned in the docs explicitly - I could imagine many other developers being confused about this :sweat_smile:

3 Likes

Thanks for the feedback!

1 Like