是否有一种直接的方法来更新MongoDB中嵌套的实体array.我使用MongoDB C# Driver
从应用程序调用DB.下面是一个exmaple:假设我有一个Student
集合,其中每个文档都有一个Course
的嵌套数组,其中填充了一些必要的字段,Course
本身是一个单独的集合,如:
{
"_id": "234dssfcv456",
"Name": "Jean Douglas",
"Age": 32,
"Courses":
[
{
"_id": "1234",
"Name": "Computer Science",
"Level": "Basic"
},
{
"_id": "3456",
"Name": "Bio Science",
"Level": "Intermediate"
}
]
}
我知道我可以通过索引更新嵌套的实体,比如下面的内容,但我不知道索引,而是只知道嵌套的Course
对象Id
.
db.College.Student.update(
{"Student._id": "234dssfcv456"},
{$set: {
"Student.$.Courses.1.Level": "Basic"
}}
Right now, am reading the entire nested array of courses -> doing the modification at application end -> then passing the entire array for update with the filedname "Courses"
which is going to replace the existing array with the one passed.
但我在想,有没有一种方法可以用Id
个实体更新数组中的一个实体.请建议.
***在Related个问题部分的右侧,所有显示了使用对象项的index更新嵌套的对象数组,这对我来说是不可能的.