我想更新此文档......
{
"_id" : "2QCTWXJHMa3Sjnta5",
"text" : [
{
"id" : "krD8yXFEEd8Esc4Qc",
"sentence" : "dasdsada."
},
{
"id" : "AwXHZmPKSPyKvd9E9",
"sentence" : "adsdadas ."
},
{
"id" : "bsf6RyKDQyrnqsmK7",
"sentence" : "daadadadad"
}
]
}
...到此
{
"_id" : "2QCTWXJHMa3Sjnta5",
"text" : [
{
"id" : "krD8yXFEEd8Esc4Qc",
"sentence" : "dasdsada."
},
{
"id" : "AwXHZmPKSPyKvd9E9",
"sentence" : "Just changed"
},
{
"id" : "bsf6RyKDQyrnqsmK7",
"sentence" : "daadadadad"
}
]
}
因此我尝试了:
Collection.update(
{ _id: '2QCTWXJHMa3Sjnta5', text.id: 'AwXHZmPKSPyKvd9E9' },
{ $set: { sentence: "Just changed" }}
);
我做错了什么?
答案 0 :(得分:1)
您需要识别数组中的元素才能更新它。您可以使用位置$ operator来执行此操作。
请注意,位置$运算符充当数组中与查询匹配的第一个元素的占位符。所以你只能用它来更新数组中第一个匹配的元素。
尝试以下查询:
Collection.update(
{ _id: '2QCTWXJHMa3Sjnta5', 'text.id': 'AwXHZmPKSPyKvd9E9' },
{ $set: { 'text.$.sentence': "Just changed" }}
);