cnak2 cnak2 - 3 years ago 169
Node.js Question

Trying to capture _id after findOneAndUpdate upsert

I'm using findOneAndUpdate with "upsert" for the first time. I'm replacing some ugly code I had initially.

I really like how clean the refactoring is, but one of my requirements is if a new record is created, I need to capture the _id for use in another process immediately following.

The problem is that if a record doesn't exist and it does an "upsert" the return is null.

I've tried returnNewDocument: true & false but no luck.

Here is how my code is structured:

{ "user_email" : req.body.userEmail},
{ $set: { "property_1" : req.body.prop1, "property_2": req.body.prop2 } },
upsert: true,
returnNewDocument: true
function(err, data){
console.log('Error in find one and update');

console.log('Show data result: ', data);


I thought changing returnNewDocument to "false" would do the trick as it should return the original document, but that doesn't work.

Any ideas?

Thanks, All!!

Answer Source
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download