node.js 如何更新猫鼬中的数组值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41501939/
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
How to update a array value in Mongoose
提问by MMR
I want to update a array value but i am not sure about the proper method to do it ,so for i tried following method but didnt worked for me.
我想更新一个数组值,但我不确定这样做的正确方法,所以我尝试了以下方法但对我不起作用。
My model, The children field in my model
我的模型,我模型中的儿童字段
childrens: {
type: Array,
default: ''
}
My query,
我的查询,
Employeehierarchy.update({ _id: employeeparent._id} ,{ $set: {"$push": { "childrens": employee._id }} })
.exec(function (err, managerparent) {});
Can anyone please provide me help.Thanks.
任何人都可以提供帮助。谢谢。
回答by chridam
You can't use both $setand $pushin the same update expression as nested operators.
您不能在与嵌套运算符相同的更新表达式中同时使用$set和$push。
The correct syntax for using the update operatorsfollows:
使用更新运算符的正确语法如下:
{
<operator1>: { <field1>: <value1>, ... },
<operator2>: { <field2>: <value2>, ... },
...
}
where <operator1>, <operator2>can be from any of the update operators list specified here.
where<operator1>, <operator2>可以来自此处指定的任何更新运算符列表。
For adding a new element to the array, a single $pushoperator will suffice e.g. you can use the findByIdAndUpdateupdate method to return the modified document as
要将新元素添加到数组中,单个$push运算符就足够了,例如您可以使用findByIdAndUpdateupdate 方法将修改后的文档返回为
Employeehierarchy.findByIdAndUpdate(employeeparent._id,
{ "$push": { "childrens": employee._id } },
{ "new": true, "upsert": true },
function (err, managerparent) {
if (err) throw err;
console.log(managerparent);
}
);
Using your original update()method, the syntax is
使用您的原始update()方法,语法是
Employeehierarchy.update(
{ "_id": employeeparent._id},
{ "$push": { "childrens": employee._id } },
function (err, raw) {
if (err) return handleError(err);
console.log('The raw response from Mongo was ', raw);
}
);
in which the callback function receives the arguments (err, raw)where
其中回调函数接收参数(err, raw),其中
erris the error if any occurredrawis the full response from Mongo
err如果发生任何错误是错误raw是来自 Mongo 的完整回应
Since you want to check the modified document, I'd suggest you use the findByIdAndUpdatefunction since the update()method won't give you the modified document, just the full write result from mongo.
既然你想查看修改后的文档,我建议你使用这个findByIdAndUpdate函数,因为该update()方法不会给你修改后的文档,只会给出mongo的完整写入结果。
If you want to update a field in the document and add an element to an array at the same time then you can do
如果要更新文档中的字段并同时向数组添加元素,则可以执行以下操作
Employeehierarchy.findByIdAndUpdate(employeeparent._id,
{
"$set": { "name": "foo" },
"$push": { "childrens": employee._id }
}
{ "new": true, "upsert": true },
function (err, managerparent) {
if (err) throw err;
console.log(managerparent);
}
);
The above will update the namefield to "foo" and add the employee id to the childrensarray.
上面的代码会将name字段更新为“foo”并将员工 ID 添加到childrens数组中。
回答by Shaishab Roy
can follow this
可以按照这个
if childrenscontains string values then model can be like:
如果childrens包含字符串值那么模型可以是这样的:
childrens: [{
type : String
}]
if childrenscontains ObjectId values of another collection _idand want populate then model can be like:
如果childrens包含另一个集合的 ObjectId 值_id并想要填充,则模型可以是:
childrens: [{
type : mongoose.Schema.Types.ObjectId,
ref: 'refModelName'
}]
no need to use $setjust use $pushto insert value in childrensarray. so query can be like:
无需使用$set仅用于$push在childrens数组中插入值。所以查询可以是这样的:
Employeehierarchy.update(
{ _id: employeeparent._id},
{"$push": { "childrens": employee._id } }
).exec(function (err, managerparent) {
//
});
回答by Deeksha Sharma
This will help I guess
这将有助于我猜
Employeehierarchy.findOneAndUpdate(
{ _id:employeeparent._id },
{ $set: { "childrens": employee._id }}
)

