本人使用的mongo为3.2.10,最新版本更新请查看官方文档
原始数据结构如下
{
"dataSourceId": "abc",
"name": "用户",
"result": [{
"title": "aa",
"type": "2"
}, {
"title": "bb",
"type": "1"
}, {
"childs": [{
"title": "aa",
"type": "2"
}, {
"title": "bb",
"type": "2"
}, {
"title": "cc",
"type": "2"
}, {
"title": "dd",
"type": "1"
}],
"title": "behaviorInfo"
}]
}
更新一级数组
更新一级数组下的type字段为1,修改的部位如下图:
修改语句:
db.dataSource.update({"dataSourceId":"abc", "result.title": {$eq : "aa"}}, {$set :{"result.$.type":"1"}})
更新二级数组
二级数组插入新值
向title为behaviorInfo的childs数组插入新的字段,修改部位如下图:
修改语句:
db.dataSource.update({"dataSourceId":"abc", "result.title": {$eq : "behaviorInfo"}}, {$push : {"result.$.childs" : {$each : [{"title":"zymdDurPerson1","type":"5"}, {"title":"txmdIsPickTerm","type":"5"}, {"title":"txmdIsSeePlan","type":"5"}, {"title":"txmdIsCashAll","type":"5"}]}}})