Ben Muircroft
Ben Muircroft

Reputation: 3034

node-mongodb show update results

When I db.collection('example').update({"a":1},{"$set":{"b":2}},{multi:true},function(e,r){

I get r:

{
n:3,
nModified:3,
ok:1
}

This works, I can see If I look at my db that I have successfully updated 3 documents but where are my results?

Quoted from https://mongodb.github.io/node-mongodb-native/markdown-docs/insert.html

callback is the callback to be run after the records are updated. Has three parameters, the first is an error object (if error occured), the second is the count of records that were modified, the third is an object with the status of the operation.

I've tried with 3 outputs in the callback but, then I just get null as a result

db.collection('example').update({"a":1},{"$set":{"b":2}},{multi:true},function(e,n,r){

My documents have been successfully updated but r is null!

I am expecting for this to return my updated documents

It doesn't look like this operation ever does, so how can I manullay return the documents that got changed?

Upvotes: 0

Views: 780

Answers (2)

irimawi
irimawi

Reputation: 368

You can use findAndModify to get the updated document in the result. It's callback has 2 parameters: 1- error 2- Updated document

I am not sure this would work for you, but check [documentation]: https://mongodb.github.io/node-mongodb-native/markdown-docs/insert.html#find-and-modify for more info.

Upvotes: 1

thoaionline
thoaionline

Reputation: 524

To get the updated documents in the returned result, you'll need to use the db.collection.bulkWrite method instead.

Upvotes: 1

Related Questions