在 MongoDB 中如何使用 $set
更新嵌套值?
例如,考虑具有以下文档的集合 people:
{
_id: ObjectId("5a7e395e20a31e44e0e7e284"),
name: "foo",
address: { street: "123", town: "bar" }
}
如何将 address
文档中嵌入的 street
字段从 "123"
更新为 "Main Street"
?
最佳答案
使用 dot notation :
db.people.update({ }, { $set: { "address.street": "Main Street" } })
https://stackoverflow.com/questions/3828452/