使用拉或推以下代码可正常工作:
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
}
});