将子文档数组元素添加到mongoDB中的子文档数组元素 [英] Add subdocument array element to subdocument array element in mongoDB
本文介绍了将子文档数组元素添加到mongoDB中的子文档数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这可能吗? 我有一个集合C,具有属性A1的数组. 每个属性都有一组子属性A2.
Is this possible? I have a collection C, with an array of attributes A1. Each attribute has an array of subattributes A2.
如何将子文档添加到特定的C.A1子文档中?
How can I add a subdocument to a specific C.A1 subdocument ?
推荐答案
这里是一个示例.
db.docs.insert({_id: 1, A1: [{A2: [1, 2, 3]}, {A2: [4, 5, 6]}]})
如果知道要插入的子文档的索引,则可以在中间使用索引(从0开始)的点符号:
If you know the index of the subdocument you want to insert, you can use dot notation with the index (starting from 0) in the middle:
db.docs.update({_id: 1}, {$addToSet: {'A1.0.A2': 9}})
结果是:
{
"A1" : [
{
"A2" : [
1,
2,
3,
9
]
},
{
"A2" : [
4,
5,
6
]
}
],
"_id" : 1
}
这篇关于将子文档数组元素添加到mongoDB中的子文档数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文