Mongo Db将元素插入到嵌套的mongo数组中

时间:2015-02-03 07:46:04

标签: php arrays mongodb

我有以下文档结构

{
   assets : {
              type : [
                       {
                        type : spread
                        scene : [
                                    {
                                      ....
                                    },
                                    {
                                      ....
                                    }

                                ] 
                       }
                     ]      
            }

}

所以我想在“scene”数组中添加新元素,并在同一文件中添加“spread”类型的匹配条件

我如何编写相同的Mongo Update查询?

1 个答案:

答案 0 :(得分:0)

以下是示例数组

{
   assets : {
              type : [
                       {
                        type : spread
                        scene : [
                                    {
                                      ....
                                    },
                                    {
                                      ....
                                    }

                                ] 
                       }
                     ]      
            }

}

参考链接 MongoDB & Meteor - Query to push into nested array does not work, no error thrown

查询在场景数组中插入新元素是

db.PhotostoryDesigner.update(
    { 
        "photostoryId":"123", 
        "assets.type.type":"spreads" 
    },
    {"$push": { 
        'assets.type.$.scene':{"$each":[
            {
              "scene_id": "1",
              "name": "Ad hoc 1",
              "spread": [
                {
                  "spreadId": "7725",
                  "name": "PAGE 1-2",
                  "asset": []
                }
              ]
            }
        ]}}
    }
);