Use the third parameter from the callback function:
...
User.update({_id: usr._id}, upsertData, {upsert: true}, function(err, num, n) {
if (err) {
res.status(500).json({});
return;
}
if (!n.updatedExisting) {
}
res.status(204).json({});
});
...
n is an object like this:
{ updatedExisting: false,
upserted: <ObjectId>,
n: 1,
connectionId: 11,
err: null,
ok: 1 }
updatedExistingproperty truewhen the document was updated, so it was created earlier. If it is false, it means that a new document was created during this call.
source
share