0

我的 node.js 应用程序中有以下代码,我可以在其中更新字段或插入新条目:

 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");
                }
            }); 

有没有办法区分是否发生了更新或插入?

4

1 回答 1

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

于 2013-07-03T15:31:40.793 回答