node.js 我如何在猫鼬中更新多个文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6694507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:22:49  来源:igfitidea点击:

how can i update multiple documents in mongoose

mongodbnode.jsmongoose

提问by pkyeck

I found the following script:

我找到了以下脚本:

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

MongoDB has the "multi" flag for an update over multiple documents but I wasn't able to get this working with mongoose. Is this not yet supported or am I doing something wrong?!

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

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

回答by kcbanner

Currently I believe that update()in Mongoose has some problems, see: https://groups.google.com/forum/#%21topic/mongoose-orm/G8i9S7E8Ergand https://groups.google.com/d/topic/mongoose-orm/K5pSHT4hJ_A/discussion.

目前我认为update()在猫鼬有一些问题,请参阅:https: //groups.google.com/forum/#%21topic/mongoose-orm/G8i9S7E8Erghttps://groups.google.com/d/topic/mongoose- orm/K5pSHT4hJ_A/讨论

However, check the docs for update: http://mongoosejs.com/docs/api.html(its under Model). The definition is:

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

Earlier Solution(Depreciated after mongoose 5+ version)

较早的解决方案(猫鼬 5+ 版本后折旧)

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

You need to pass the options inside an object, so your code would be:

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

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

New Solution

新解决方案

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

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

I believe that Mongoose wraps your cid in a $set, so this is not the same as running that same update in the mongo shell. If you ran that in the shell then all documents would be replaced by one with a single cid: ''.

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

回答by Moh .S

You have to use the multi: true option

您必须使用 multi: true 选项

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

回答by Serdar De?irmenci

Those answers are deprecated. This is the actual solution:

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

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

回答by sina

as mentioned in the mongoose documentsthis is how we do this:

正如猫鼬文档中提到的,这就是我们这样做的方式:

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

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

so this is an example based on the docs:

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

    // 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('/');
          }
        }
      });

this worked fine for me, I hope it helps :)

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

回答by mamane man

as @sina mentioned:

正如@sina 提到的:

let conditions = {};
let options = { multi: true, upsert: true };

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

you can add a callback function after optionsbut it's not necessary.

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