English 中文(简体)
我如何在县更新多份文件?
原标题:How can I update multiple documents in mongoose?

我发现以下文字:

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

Mongo 亚洲开发银行拥有多个文件更新的“多”旗帜,但我无法与大湖合作。 难道这还没有得到支持,或者我是否做过错呢?

Device.update({}, {cid:   }, false, true, function (err) {
  //...
});
最佳回答
问题回答

Those answers are deprecated. This is the actual solution:

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

你们必须使用多种选择:真正的选择

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

参看mongoose documents。 这就是我们如何做到这一点:

<编码>db. Collection.updateMany(condition, Update, Options,etback 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( / );
          }
        }
      });

这对我来说是出色的。

await Device.updateMany({_id: {$in: cid}},{ $set: {columnNameHere: "columnValueHere"}},{multi:true,upsert: true,new: true});

参看@sina提到:

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

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

可在<条码>后添加一个接线功能<>但并非必要。

你可以尝试以下方式:

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




相关问题
Access DB Ref MongoDB

Whats the best way to access/query a DB Ref: UPDATE: users: name, groupref : {$ref:"groups",$id:"ObjectId ..." } } groups: name, topic, country,...,.. Assumption is that user belongs to only one ...

MongoDB nested sets

What re the best practices to store nested sets (like trees of comments) in MongoDB? I mean, every comment can have a parent comment and children-comments (answers). Storing them like this: { ...

MongoMapper and migrations

I m building a Rails application using MongoDB as the back-end and MongoMapper as the ORM tool. Suppose in version 1, I define the following model: class SomeModel include MongoMapper::Document ...

MongoDB takes long for indexing

I have the following setup: Mac Pro with 2 GB of RAM (yes, not that much) MongoDB 1.1.3 64-bit 8 million entries in a single collection index for one field (integer) wanted Calling .ensureIndex(...) ...

Storing and accessing large amounts of data

My application creates pieces of data that, in xml, would look like this: <resource url="someurl"> <term> <name>somename</name> <frequency>somenumber</...

热门标签