[英]How can I update multiple documents in mongoose?
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. MongoDB 具有用于更新多个文档的“multi”标志,但我无法与 mongoose 一起使用。 Is this not yet supported or am I doing something wrong?
这还不支持还是我做错了什么?
Device.update({}, {cid: ''}, false, true, function (err) {
//...
});
Currently I believe that update()
in Mongoose has some problems, see: https://groups.google.com/forum/#%21topic/mongoose-orm/G8i9S7E8Erg and https://groups.google.com/d/topic/mongoose-orm/K5pSHT4hJ_A/discussion . Currently I believe that
update()
in Mongoose has some problems, see: https://groups.google.com/forum/#%21topic/mongoose-orm/G8i9S7E8Erg and https://groups.google.com/d/topic /mongoose-orm/K5pSHT4hJ_A/讨论。
However, check the docs for update: http://mongoosejs.com/docs/api.html (its under Model).但是,请检查文档以获取更新: http://mongoosejs.com/docs/api.html (在模型下)。 The definition is:
定义是:
Earlier Solution(Depreciated after mongoose 5+ version)早期解决方案(mongoose 5+ 版本后贬值)
Model.update = function (query, doc, options, callback) { ... }
You need to pass the options inside an object, so your code would be:您需要在 object 中传递选项,因此您的代码将是:
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.我相信 Mongoose 将您的 cid 包装在 $set 中,因此这与在 mongo shell 中运行相同的更新不同。 If you ran that in the shell then all documents would be replaced by one with a single
cid: ''
.如果您在 shell 中运行它,那么所有文档都将被替换为一个带有单个
cid: ''
文档。
Those answers are deprecated.这些答案已被弃用。 This is the actual solution:
这是实际的解决方案:
Device.updateMany({}, { cid: '' });
You have to use the multi: true option您必须使用 multi: true 选项
Device.update({},{cid: ''},{multi: true});
as mentioned in the mongoose documents this is how we do this:如mongoose 文档中所述,我们是这样做的:
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:)这对我来说很好,我希望它有帮助:)
as @sina mentioned:正如@sina 提到的:
let conditions = {};
let options = { multi: true, upsert: true };
Device.updateMany(conditions , { cid: '' },options );
you can add a callback function after options
but it's not necessary.您可以在
options
后添加回调 function 但这不是必需的。
You can try the following way您可以尝试以下方式
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
})
}
await Device.updateMany({_id: {$in: cid}},{ $set: {columnNameHere: "columnValueHere"}},{multi:true,upsert: true,new: true});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.