0

如果我有一个看起来像这样的文档:

{ 
    "_id" : 1, 
    "name": "Homer J. Simpson", 
    "income" : 45000,
    "address": { 
        "street": "742 Evergreen Terrace", 
        "city": "Springfield", 
        "state": "???", 
        "email": "homer@springfield.com", 
        "zipcode": "12345", 
        "country": "USA"
     }
}

并希望对地址文档中的某些字段进行更新(保持其他字段不变),如果它们尚不存在,则插入新字段,例如:

{ 
    "address": {
        "email": "homer@gmail.com", 
        "zipcode": "77788", 
        "latitude" : 23.43545, 
        "longitude" : 123.45553
    }
}

有没有办法一次进行一次原子更新,或者您是否需要遍历新数据中的键/值并.update()为每个值执行一次?

4

2 回答 2

1

将点符号与 a$set一起使用以在单个 中定位多个嵌入字段update

{ "$set": {
    "address.email": "homer@gmail.com", 
    "address.zipcode": "77788", 
    "address.latitude" : 23.43545, 
    "address.longitude" : 123.45553
} }
于 2013-10-01T16:07:54.053 回答
0

As Sergio metioned use a $set.

{address.latitude : "77788"}
于 2013-10-01T15:55:44.750 回答