我已经文档结构化等,从而更新在数组中的子文档:MongoDB的:使用索引
{
key: "apples002",
main: [
{q: "Is the apple green?", a1: "The apple is not green.", a2: "No."},
{q: "What color is the apple?", a1: "The apple is yellow.", a2: "Yellow."}
],
alt: [
{q: "What color is the apple?", a1: "The apple is red."},
{q: "Is the apple yellow?", a1: “The apple is yellow.”, a2: “Yes.”}
]
}
我看到用于更新子文档字段几个实例中,但最重要的其中的子文件具有唯一的ID。我还学会了如何通过索引来引用其中一个子文档,例如更新上述主(第一元件)在q字段:
myDB.update({key: 'apples002'}, {$set: {'main.0.q': 'Updated question goes here'}})
在我的情况
所以,我想在代替上述数组的索引0的使用可变。我试图用正确的字符串值创建一个局部变量,并使用它来代替上面的'main.0.q',但那不起作用。有任何想法吗?
[在mongodb更新中使用变量]可能的重复(http://stackoverflow.com/questions/12393351/using-a-variable-in-mongodb-update) – JohnnyHK