【问题标题】:How can I update multiple documents in mongoose?如何更新猫鼬中的多个文档?
【发布时间】:2011-10-05 09:19:54
【问题描述】:

我找到了以下脚本:

Device.find(function(err, devices) {
  devices.forEach(function(device) {
    device.cid = '';
    device.save();
  });
});

MongoDB 具有用于更新多个文档的“multi”标志,但我无法与 mongoose 一起使用。这还不支持还是我做错了什么?

Device.update({}, {cid: ''}, false, true, function (err) {
  //...
});

【问题讨论】:

    标签: mongodb node.js mongoose


    【解决方案1】:

    目前我认为Mongoose中的update()有一些问题,见: https://groups.google.com/forum/#%21topic/mongoose-orm/G8i9S7E8Erghttps://groups.google.com/d/topic/mongoose-orm/K5pSHT4hJ_A/discussion

    但是,请检查文档以获取更新:http://mongoosejs.com/docs/api.html(在模型下)。定义是:

    早期解决方案(mongoose 5+版本后折旧)

    Model.update = function (query, doc, options, callback) { ... }
    

    您需要在对象中传递选项,因此您的代码将是:

    Model.update = function ({}, {cid: ''}, {multi: true}, function(err) { ... });
    

    新解决方案

    Model.updateMany = function (query, doc, callback) { ... }
    
    Model.updateMany = function ({}, {cid: ''}, function(err) { ... });
    

    我相信 Mongoose 将您的 cid 包装在 $set 中,因此这与在 mongo shell 中运行相同的更新不同。如果您在 shell 中运行它,那么所有文档都将替换为一个带有单个 cid: '' 的文档。

    【讨论】:

    • 这正是我想要的......将所有设备的 cid 更新为“”。谢谢
    • 为什么要给我们看函数定义?认为您可能会建议我们覆盖自定义函数是一种误导,您是否应该这样打印它:Device.updateMany({}, { cid: '' }); 就像@Moh .S 回答的那样?
    【解决方案2】:

    这些答案已被弃用。这是实际的解决方案:

    Device.updateMany({}, { cid: '' });
    

    【讨论】:

      【解决方案3】:

      你必须使用 multi:true 选项

      Device.update({},{cid: ''},{multi: true});
      

      【讨论】:

        【解决方案4】:

        正如mongoose documents 中提到的那样,我们就是这样做的:

        db.collection.updateMany(condition, update, options, callback function)

        所以这是一个基于文档的示例:

            // creating arguments
            let conditions = {};
            let update = {
                $set : {
              title : req.body.title,
              description : req.body.description,
              markdown : req.body.markdown
              }
            };
            let options = { multi: true, upsert: true };
        
            // update_many :)
            YourCollection.updateMany(
        
              conditions, update, options,(err, doc) => {
                console.log(req.body);
                if(!err) {
                  res.redirect('/articles');
                }
                else {
                  if(err.name == "ValidationError"){
                    handleValidationError(err , req.body);
                    res.redirect('/new-post');
                  }else {
                    res.redirect('/');
                  }
                }
              });
        

        这对我来说很好,我希望它有帮助:)

        【讨论】:

        • 这是唯一有效的答案,但它只有 0 票?由于某种原因,它只适用于回调,这很奇怪
        【解决方案5】:

        正如@sina 所说:

        let conditions = {};
        let options = { multi: true, upsert: true };
        
        Device.updateMany(conditions , { cid: '' },options );
        

        你可以在options之后添加回调函数,但这不是必须的。

        【讨论】:

          【解决方案6】:

          你可以试试下面的方法

          try {
              const icMessages = await IcMessages.updateMany({
                  room: req.params.room
              }, {
                  "$set": {
                      seen_status_2: "0"
                  }
              }, {
                  "multi": true
              });
              res.json(icMessages)
          
          } catch (err) {
              console.log(err.message)
              res.status(500).json({
                  message: err.message
              })
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-10-18
            • 2018-01-22
            • 2020-06-27
            • 2018-08-20
            • 2018-09-08
            • 1970-01-01
            • 1970-01-01
            • 2013-12-04
            相关资源
            最近更新 更多