Makaille
Makaille

Reputation: 1666

get update value after an updateOne()

is it possible to get all updated value/value where there is a diff after a call of updateOne()

 _.each(gameResult, function(gameResult){
    bulk.find({"user" : gameResult.user, "section" : gameResult.section , "code" : gameResult.code}).upsert().updateOne(gameResult);
  })

imagine my value in the db before the update are :

1, 1, 1, 1

gameResult is :

1, 2, 1 , 3

So my db is updated : 1, 2, 1, 3

Is it possible to get all value who are changed, so here how can i get my two object 2 and 3 cause i need it in the code.

Upvotes: 2

Views: 3736

Answers (1)

Philipp
Philipp

Reputation: 69783

You are looking for findAndModify.

This performs an update while also returning the existing document from the database. By default, this method returns the document before the changes are applied (this behavior can be changed with the new:true option). So you can easily compare the returned document with your local document to find the differences.

Unfortunately findAndModify isn't supported by bulk operations, so you need to do it document-by-document.

Upvotes: 3

Related Questions