bri*_*dip 5 mongoose mongodb node.js meanjs
我在 mongoose 中定义了一个架构,如下所示:
var VolunteerSchema = new Schema ({
......
other fields
.....
preferLocations:[{
type: Schema.ObjectId,
ref: 'Location'
}]
.....
});
Run Code Online (Sandbox Code Playgroud)
我正在使用volunteer.save()方法来更新模型。
在更新volunteer模型时,我收到如下错误:
{ [DivergentArrayError: For your own good, using `document.save()` to update an
array which was selected using an $elemMatch projection OR populated using skip,
limit, query conditions, or exclusion of the _id field when the operation resul
ts in a $pop or $set of the entire array is not supported. The following path(s)
would have been modified unsafely:
preferLocations
Use Model.update() to update these arrays instead.]
message: 'For your own good, using `document.save()` to update an array which
was selected using an $elemMatch projection OR populated using skip, limit, quer
y conditions, or exclusion of the _id field when the operation results in a $pop
or $set of the entire array is not supported. The following path(s) would have
been modified unsafely:\n preferLocations\nUse Model.update() to update these a
rrays instead.',
name: 'DivergentArrayError' }
Run Code Online (Sandbox Code Playgroud)
在更新位置时,我收集数组中的 _ids 字段并分配给 preferredLocations,如下所示:
volunteer.preferLocations = locationIdsArray;
Run Code Online (Sandbox Code Playgroud)
删除此行时我没有收到错误消息。我做错了什么?
在投影中使用时$elemMatch,请勿使用document.save(). 相反,请使用 手动更新您的文档Model.update()。在你的情况下你应该尝试
volunteer.findOneAndUpdate(
{
_id: ObjectId("567452bae5b25d6e6c1a0f7e"),
localization: { '$elemMatch': { language: 'de' } }
},
{
$set: { 'localization.$.name' : "Neuer Name" }
}).exec(//...
});
Run Code Online (Sandbox Code Playgroud)
点击此处了解更多详情
| 归档时间: |
|
| 查看次数: |
824 次 |
| 最近记录: |