【发布时间】:2020-08-12 22:12:03
【问题描述】:
我有一个这样的架构:
const NoteClusterSchema = new Schema({
noteInfo: [{
title: String,
Description: String,
authors:[{
name: String, Id: String
}]
}],
idGroup: String //this is the group of the people that are working on the proyect
}, {timestamps:true});
noteInfo 的一个例子是
noteInfo: [{
title: "Hello",
Description: "Hello World",
authors:[
{name: "Marcelo", id: "1234123123"},
{name: "Pancho", id: "12341345344"}
]
}]
例如,如何在作者数组中更新“Marcelo”的名称? (假设如果用户更改了它的名称。我知道在这种特定情况下我可以只放置 _id,但在我的真实架构中它不是我想要更新的用户)
我想到了这样的事情:
await NoteClusterSchema.updateMany({idGroup: req.body.idGroup},
{
noteInfo: {$push: {authors: {name:
req.body.name}}}
})
但是通过这种方式,它不会知道所有noteInfo数组中的哪一个更新或者它会创建一个新的authors数组,而更改noteInfo: {$push:$push: {noteInfo:只会创建一个新的noteInfo数组。
那么,如何使用 UpdateMany 方法更新另一个数组中的一个数组中的这个特定对象?
【问题讨论】:
-
这能回答你的问题吗? Update nested array element in mongodb
标签: arrays mongodb mongoose mern