mongodb update()仅修改第一个找到的值

时间:2014-10-12 14:37:59

标签: mongodb

请建议哪个命令更适合更新多个字段,或者下面的查询有什么问题。 查询更新仅限价值:23

db.products.update( {"for":"ac3"}, {$inc:{"price":2}} )

> db.products.find({},{for:1,price:1})
{ "_id" : "ac3", "price" : 200 }
{ "_id" : "ac7", "price" : 320 }
{ "_id" : ObjectId("507d95d5719dbef170f15bf9"), "price" : 23, "for" : [ "ac3", "ac7", "ac9" ] }
{ "_id" : ObjectId("507d95d5719dbef170f15bfa"), "price" : 12 }
{ "_id" : ObjectId("507d95d5719dbef170f15bfb"), "price" : 38, "for" : [ "ac3", "ac7", "ac9", "qp7",
"qp8", "qp9" ] }
{ "_id" : ObjectId("507d95d5719dbef170f15bfc"), "price" : 12.5, "for" : "ac3" }
{ "_id" : ObjectId("507d95d5719dbef170f15bfd"), "price" : 12, "for" : "ac3" }
{ "_id" : ObjectId("507d95d5719dbef170f15bfe") }
{ "_id" : ObjectId("507d95d5719dbef170f15bff") }
{ "_id" : ObjectId("507d95d5719dbef170f15c00") }
{ "_id" : ObjectId("507d95d5719dbef170f15c01") }
{ "_id" : "ac9", "price" : 333 }

1 个答案:

答案 0 :(得分:2)

您需要将multi参数设置为true

db.products.update({ "for": "ac3" }, { $inc: { "price": 2 } }, { "multi": true });