如何更新 Mongoose 中的数组值
How to update a array value in Mongoose
我想更新一个数组值,但我不确定这样做的正确方法,所以我尝试了以下方法但对我没有用。
我的模特,
我模型中的 children 字段
childrens: {
type: Array,
default: ''
}
我的查询,
Employeehierarchy.update({ _id: employeeparent._id} ,{ $set: {"$push": { "childrens": employee._id }} })
.exec(function (err, managerparent) {});
谁能提供给我help.Thanks.
您不能在同一个更新表达式中同时使用 $set
and $push
作为嵌套运算符。
使用 update operators 的正确语法如下:
{
<operator1>: { <field1>: <value1>, ... },
<operator2>: { <field2>: <value2>, ... },
...
}
其中 <operator1>, <operator2>
可以来自任何指定的更新运算符列表 here。
为了向数组添加新元素,单个 $push
operator will suffice e.g. you can use the findByIdAndUpdate
更新方法 return 修改后的文档为
Employeehierarchy.findByIdAndUpdate(employeeparent._id,
{ "$push": { "childrens": employee._id } },
{ "new": true, "upsert": true },
function (err, managerparent) {
if (err) throw err;
console.log(managerparent);
}
);
使用你原来的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);
}
);
其中回调函数接收参数 (err, raw)
where
err
是错误,如果有的话
raw
是 Mongo 的完整回复
既然要查看修改后的文档,建议使用findByIdAndUpdate
函数,因为update()
方法不会给你修改后的文档,只是 mongo 的完整写入结果。
如果您想更新文档中的字段并同时向数组添加元素,那么您可以这样做
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);
}
);
以上将 name
字段更新为 "foo" 并将员工 ID 添加到 childrens
数组。
可以关注这个
如果 childrens
包含字符串值,那么模型可以是这样的:
childrens: [{
type : String
}]
如果 childrens
包含另一个集合的 ObjectId 值 _id
并且想要填充,那么模型可以是这样的:
childrens: [{
type : mongoose.Schema.Types.ObjectId,
ref: 'refModelName'
}]
无需使用 $set
只需使用 $push
将值插入 childrens
数组。所以查询可以是这样的:
Employeehierarchy.update(
{ _id: employeeparent._id},
{"$push": { "childrens": employee._id } }
).exec(function (err, managerparent) {
//
});
我猜这会有所帮助
Employeehierarchy.findOneAndUpdate(
{ _id:employeeparent._id },
{ $set: { "childrens": employee._id }}
)
我想更新一个数组值,但我不确定这样做的正确方法,所以我尝试了以下方法但对我没有用。
我的模特, 我模型中的 children 字段
childrens: {
type: Array,
default: ''
}
我的查询,
Employeehierarchy.update({ _id: employeeparent._id} ,{ $set: {"$push": { "childrens": employee._id }} })
.exec(function (err, managerparent) {});
谁能提供给我help.Thanks.
您不能在同一个更新表达式中同时使用 $set
and $push
作为嵌套运算符。
使用 update operators 的正确语法如下:
{
<operator1>: { <field1>: <value1>, ... },
<operator2>: { <field2>: <value2>, ... },
...
}
其中 <operator1>, <operator2>
可以来自任何指定的更新运算符列表 here。
为了向数组添加新元素,单个 $push
operator will suffice e.g. you can use the findByIdAndUpdate
更新方法 return 修改后的文档为
Employeehierarchy.findByIdAndUpdate(employeeparent._id,
{ "$push": { "childrens": employee._id } },
{ "new": true, "upsert": true },
function (err, managerparent) {
if (err) throw err;
console.log(managerparent);
}
);
使用你原来的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);
}
);
其中回调函数接收参数 (err, raw)
where
err
是错误,如果有的话raw
是 Mongo 的完整回复
既然要查看修改后的文档,建议使用findByIdAndUpdate
函数,因为update()
方法不会给你修改后的文档,只是 mongo 的完整写入结果。
如果您想更新文档中的字段并同时向数组添加元素,那么您可以这样做
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);
}
);
以上将 name
字段更新为 "foo" 并将员工 ID 添加到 childrens
数组。
可以关注这个
如果 childrens
包含字符串值,那么模型可以是这样的:
childrens: [{
type : String
}]
如果 childrens
包含另一个集合的 ObjectId 值 _id
并且想要填充,那么模型可以是这样的:
childrens: [{
type : mongoose.Schema.Types.ObjectId,
ref: 'refModelName'
}]
无需使用 $set
只需使用 $push
将值插入 childrens
数组。所以查询可以是这样的:
Employeehierarchy.update(
{ _id: employeeparent._id},
{"$push": { "childrens": employee._id } }
).exec(function (err, managerparent) {
//
});
我猜这会有所帮助
Employeehierarchy.findOneAndUpdate(
{ _id:employeeparent._id },
{ $set: { "childrens": employee._id }}
)