我已經文檔結構化等,從而更新在數組中的子文檔: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