参考地址 Mongodb字段更新$set操作符
{ $set: { <field1>: <value1>, ... } }
使用点号指定一个内嵌文档的字段;
{ _id: 100, sku: "abc123", quantity: 250, instock: true, reorder: false, details: { model: "14Q2", make: "xyz" }, tags: [ "apparel", "clothing" ], ratings: [ { by: "ijk", rating: 4 } ] }
设置顶级字段
db.products.update( { _id: 100 }, { $set: { quantity: 500, details: { model: "14Q3", make: "xyz" }, tags: [ "coats", "outerwear", "clothing" ] } } )
上面的操作替换quantity的值为500,details为一个新的内嵌文档,tags为一个新的数组;
db.products.update( { _id: 100 }, { $set: { "details.make": "zzz" } } )
db.products.update( { _id: 100 }, { $set: { "tags.1": "rain gear", "ratings.0.rating": 2 } } )