0

I have the following code in my node.js application where I either update a field or insert a new entry:

 DocPrivilege.update({
                _id: {
                    $in: user.documentsPriv
                }
            }, {
                $set: {
                    access: parseInt(req.body.access),
                    documentName: req.body.documentName,
                    documentId: req.body.documentId
                }
            }, {
                upsert: true
            }, function(err, updated) {
                if (err || !updated) console.log("User not updated");
                else {
                    console.log("User updated");
                }
            }); 

Is there a way how I can differentiate if an update or an insertion occured?

tune2fs
  • 7,605
  • 5
  • 41
  • 57

1 Answers1

2

You have to use db.getLastError().

It gives you an object with several properties. The property "n" gives you the number of documents updated.

Here's some documentation about it, and here's an example of getLastError in node.js

AntonioOtero
  • 1,759
  • 1
  • 14
  • 16