user1063287
user1063287

Reputation: 10879

What should findOneAndUpdate() return using Node.js 18.18.1 and MongoDB 7.0.2 and Node native driver 6.2.0?

Running this in my Node.js applicaton:

var topic = await collection.findOneAndUpdate(filter, update);

console.log(topic); 

seems to return the document object directly.

However the docs here:

https://mongodb.github.io/node-mongodb-native/6.2/classes/Collection.html#findOneAndUpdate

https://mongodb.github.io/node-mongodb-native/6.2/interfaces/ModifyResult.html

seem to suggest that the result should consist of three properties:

The native driver docs:

https://www.mongodb.com/docs/drivers/node/current/

suggest that 6.2 is the current version of the driver.

My package.json has:

"mongodb": "^6.2.0",

And to double-check my app is indeed using 6.2:

npm list mongodb
[email protected] C:\Users\me\Documents\REPOS\my application
└── [email protected]

MongoDB version is:

mongosh
Current Mongosh Log ID: ****
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.0.2
Using MongoDB:          7.0.2
Using Mongosh:          2.0.2

Edit:

Interestingly, mongoshell returns the document directly, and this is what I am experiencing when using the native driver:

https://www.mongodb.com/docs/manual/reference/method/db.collection.findOneAndUpdate/

Upvotes: 0

Views: 49

Answers (1)

user1063287
user1063287

Reputation: 10879

I think I have found the explanation here:

https://github.com/mongodb/node-mongodb-native/releases/

☀️ API usability improvements findOneAndX family of methods will now return only the found document or null by default (includeResultMetadata is false by default) Previously, the default return type of this family of methods was a ModifyResult containing the found document and additional metadata. This additional metadata is unnecessary for the majority of use cases, so now, by default, they will return only the found document or null.

The previous behavior is still available by explicitly setting includeResultMetadata: true in the options.

See the following blog post for more information.

I could be wrong, but it seems the docs are wrong in this instance (or I am reading them wrong).

Upvotes: 0

Related Questions