MongoDB - Update objects in a document's array (nested updating)

后端 未结 3 1514

Assume we have the following collection, which I have few questions about:

{
    \"_id\" : ObjectId(\"4faaba123412d654fe83hg876\"),
    \"user_id\" : 123456         


        
3条回答
  •  粉色の甜心
    2020-11-22 02:24

    We can use $set operator to update the nested array inside object filed update the value

    db.getCollection('geolocations').update( 
       {
           "_id" : ObjectId("5bd3013ac714ea4959f80115"), 
           "geolocation.country" : "United States of America"
       }, 
       { $set: 
           {
               "geolocation.$.country" : "USA"
           } 
        }, 
       false,
       true
    );
    

提交回复
热议问题