推送到基于 mongodb 的列表

push to list based mongodb

我想更新以下结构:

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'] 我想要:

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':{...}
          }

有没有一种无需太多操作即可快速完成此操作的方法?

您可以按如下方式使用dot notation to specify the embedded document in your $set

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