带有条件“全部”的updateMany无法更新数组字段

时间:2018-07-11 03:03:00

标签: node.js mongoose

使用拉或推以下代码可正常工作:

var newRole = {
  roleID: req.body.roleID,
  roleName: req.body.roleName,
  checkbox: false
};
models.Alerts.updateMany({}, { $push: {
  "whoCanSendReceive.sendReal": newRole,
  "whoCanSendReceive.receiveReal": newRole,
  "whoCanSendReceive.sendDrill": newRole,
  "whoCanSendReceive.receiveDrill": newRole
  }}, function(err, alerts) {
  console.log('alerts with new role = ',alerts);
});

我需要使用$set从roleID和roleName更新数组字段

我的模特:

var AlertsSchema = new mongoose.Schema({
whoCanSendReceive: {
  sendReal: [{
    roleID: Number,
    roleName: String,
    checkbox: { type: Boolean, default: false }
  }],
  receiveReal: [{
    roleID: Number,
    roleName: String,
    checkbox: { type: Boolean, default: false }
  }]
}, {collection:"Alerts"});
var Alerts;
module.exports.Alerts = mongoose.model("Alerts", AlertsSchema);

我的功能:

models.Alerts.updateMany({}, {"$set": {
  "whoCanSendReceive.sendReal.roleID": req.body.roleID,
  "whoCanSendReceive.receiveReal.roleName": req.body.roleName,
  "whoCanSendReceive.sendDrill.roleID": req.body.roleID,
  "whoCanSendReceive.receiveDrill.roleName": req.body.roleName
}
});

0 个答案:

没有答案