In MongoDB how do you use $set to update a nested value/embedded document?

后端 未结 2 2021
你的背包
你的背包 2020-12-09 15:08

In MongoDB how do you use $set to update a nested value?

For example, consider a collection people with the following

相关标签:
2条回答
  • 2020-12-09 15:10

    Using the dot notation:

    db.people.update({ }, { $set: { "address.street": "Main Street" } })
    
    0 讨论(0)
  • 2020-12-09 15:30

    In addition to Niels' answer, also do verify the "type" of the nested value. In my case, it was a "string" formed from json. Though this might be unlikely, but do ensure that the value has the right type.

    0 讨论(0)
提交回复
热议问题