Adding to Nested Array in MongoDB

时间:2015-05-12 23:43:05

标签: arrays mongodb

Suppose I had three arrays nested in a document as follows and I knew all the indices. How would I add another element to the third array, in this case "array3"? I wouldn't be able to use the positional operator but is there a way to update/push when you know the indices?

-VisibleProviders

1 个答案:

答案 0 :(得分:0)

好。我想到了。我不知道错过了直接通过索引引用它。我认为这是可能的,但几乎每个例子都显示位置参考,我找不到直接执行它的语法。这很简单。要在知道索引时添加到上面的数组3:

db.collection('productlist').update({name1:'foo1'},{'$push': { "array1.0.array2.0.array3": {"data": "yay"}}}