db.collection.find().pretty()
{"_id" : ObjectId("5836b428d7e99005c2585b08"), "services" : [{"type" : "Financial Transaction"}],"outStandingAmount" : 0}
{"_id" : ObjectId("5836c5a9d7e99005c2585b09"), "services" : [{"type" : "Financial Transaction"}], "outStandingAmount" : 2000}
下面的查询将做匹配的记录更新。
db.collection.updateMany({"services.type":"Financial Transaction"}, {$set:{
"services":[{"type":"Financial Trans"}]}});
因为我们有我们的收集 两个匹配记录上述updateMany查询的结果是
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
而如果我们用
db.getCollection('user_informations').updateMany({"services.type":"Financial Transaction"},{$set: {"services.type":"Financial Trans"}})
,我们会得到一个错误味精
"errmsg" : "cannot use the part (services of services.type) to traverse the element ({services: [ { type: \"Financial Transaction\" } ]})"
当涉及数组时,则'。'用于遍历数组元素,即使我们可以使用'。'遍历嵌套数组,但是对于修改数组我们有一组运算符,请参阅https://docs.mongodb.com/v3.2/reference/operator/update-array/
也'。'在使用位置运算符$时用于更新查询。
你能提供错误信息吗? – cjungel