【问题标题】:Mongo Db insert element into nested mongo arrayMongodb将元素插入嵌套的mongo数组
【发布时间】:2015-04-02 08:16:49
【问题描述】:

我有以下文档结构

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

                                ] 
                       }
                     ]      
            }

}

所以我想在同一文档中添加匹配条件为“spread”类型的“场景”数组中的新元素

我该如何编写 Mongo 更新查询?

【问题讨论】:

    标签: php arrays mongodb


    【解决方案1】:

    以下是示例数组

    {
       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": []
                    }
                  ]
                }
            ]}}
        }
    );
    

    【讨论】:

      猜你喜欢
      • 2015-08-24
      • 2016-07-06
      • 1970-01-01
      • 2018-10-06
      • 2017-01-07
      • 2017-01-05
      • 2022-01-22
      • 1970-01-01
      • 2021-01-13
      相关资源
      最近更新 更多