mongodb中存储着类似结构的数据:
{
_id:"10001",
array:[
{sub_id:"1",sub_array:["text11","text12","text13"]},
{sub_id:"2",sub_array:["text21","text22","text23"]},
{sub_id:"3",sub_array:["text31","text32","text33"]}
]
}
直接向array中添加内容的话,很简单,利用$push操作符即可,
但如果我想往sub_id为2的sub_array中push若干个字符串,应该如何定位?
Can’t do it.
Must first
find
,根据查询结果来定位sub_id
是数组的哪一个(假设如题目中所写下标是 1),最后更新{"array.1.sub_array": {$push: "text24"}}
.