推送基于列表的mongodb

时间:2016-12-15 15:49:03

标签: mongodb pymongo

我想更新以下结构:

record = {'_id':some_id,
          'status':  { 1 : {
                              'events' : [a,b,c ],    
                              'other_stuff' : { }
                             }

                     { 2 : {
                             'events' :  [a,b,c ] },    
                             'other_stuff' : { } 
                            } ,
                     }
          ,
          'other_key':{...}
          }

现在我要做的是,状态代码= 3和事件列表= [' x',' y',' z'] I我想拥有:

record = {'_id':some_id,
         'status':  { 1 : {
                              'events' : [a,b,c],    
                              'other_stuff' : { }
                             },

                     { 2 : {
                             'events' :  [a,b,c ] },    
                             'other_stuff' : { } 
                            } ,
                     { 3 : {
                             'events' :  [x,y,z ] },    

                            } ,
                     }
          ,'other_key':{...}
          }

有没有一种快速的方法可以在没有太多机动的情况下完成这项工作?

1 个答案:

答案 0 :(得分:1)

您可以使用 dot notation 中的specify the embedded document$set,如下所示:

db.collection.updateOne(
    { "_id": ObjectId("5852bc9eade47a3353ff01d0") },
    { 
        "$set": {
            "status.3": {
                "events" : ["x","y","z"]
            }
        } 
    }
)