简体   繁体   English

如何知道Mongoose的upsert是否创建了一个新文档?

[英]How to know if Mongoose's upsert created a new document?

I have this code in node.js/express.js: 我在node.js / express.js中有这个代码:

var User = mongoose.model('User');
var usersRouter = express.Router();
usersRouter.put('/:id', function(req, res) {
    req.body._id = req.params.id;
    var usr = new User(req.body);

    usr.validate(function (err) {
        if (err) {
            res.status(400).json({});
            return;
        }

        var upsertData = usr.toObject();
        delete upsertData._id;

        User.update({_id: usr._id}, upsertData, {upsert: true}, function(err) {
            if (err) {
                res.status(500).json({});
                return;
            }

            res.status(204).json({});
        });
    });
});

It works fine, but I would like to send a different response to the client if a new document has been created (status 201 with json in response body) or an existing one has been updated (status 204). 它工作正常,但我想在创建新文档时向客户端发送不同的响应(状态201,响应正文中为json)或现有的已更新(状态204)。

Is there a way to tell the difference from the callback of User.update ? 有没有办法区分User.update的回调?

Use the third parameter from callback function: 使用回调函数中的第三个参数:

...
User.update({_id: usr._id}, upsertData, {upsert: true}, function(err, num, n) {
  if (err) {
     res.status(500).json({});
     return;
   }

   if (!n.updatedExisting) { 
       /* new document */
   }

   res.status(204).json({});
});
...

n is an object like this: n是这样的对象:

{ updatedExisting: false,
  upserted: <ObjectId>,
  n: 1,
  connectionId: 11,
  err: null,
  ok: 1 }

updatedExisting property is true when a document was updated -- so it was created before. 更新文档时, updatedExisting属性为true - 因此它是在之前创建的。 If it's false , then it means that the new document was created during this call. 如果它为false ,则表示在此调用期间创建了新文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Mongoose 中更新/插入文档? - How do I update/upsert a document in Mongoose? 如果不在新文档中,Upsert会删除该字段 - Upsert removes field if it's not in the new document 如何知道upsert是否成功以及我能否获得_id更新/新插入的文档 - How to know if upsert was successful and if I can get _id of updated/new inserted document 如何使用 mongoose 为已创建的文档编写子文档 - How to write subdocuments for an already created document with mongoose 如何仅返回在使用 mongoose 执行 save() 后创建的文档 - How return only document created after perform save() with mongoose Mongoose:如何将之后创建的文档中的数据传递到之前创建的文档中? - Mongoose: How do I pass data from a document created afterwards into a document created right before? 如何知道用document.createElement创建的元素是否仍然存在 - How to know if element created with document.createElement still exists 如何与 MongoDb 同步数据并了解文档是否成功创建/更新 - How to sync data with MongoDb and know if a document is created/updated sucessfully 使用 Mongo/Mongoose,为什么在将文档添加到现有集合时会创建一个全新的数据库? - Using Mongo/Mongoose, why is an entirely new database created when adding a document to an existing collection? Mongoose - 更新文档的 map - Mongoose - Update a document's map
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM