2

我有示例产品表,并想使用 _id 字段更新多个文档。每次,我都尝试这样做,它只更新我提到的 $in 子句中的第一个文档,而不是全部更新。

db.products.update({_id:{$in:[ObjectId("507d95d5719dbef170f15bff"),
ObjectId("507d95d5719dbef170f15c01"), ObjectId("507d95d5719dbef170f15c00")]}},
{$set:{'monthly_price':7865}}, {multi:true})
4

3 回答 3

0

试试这个:

db.<collection>.update( { query }, {$set: {monthly_price:7865}}, false, true)
于 2013-07-25T05:58:57.077 回答
0

您可以先尝试在 products 表上运行 find 以确保所有对象 ID 确实存在。

您也可以尝试解释命令

于 2013-07-25T05:52:03.417 回答
0

I think the object id's which you have given doesn't exist in the collection.
I tried using the following query and it worked for me.

db.test.update({_id:{$in:[ObjectId("57b33483e5b9ce24f4910855"),
                          ObjectId("57b33483e5b9ce24f4910856"),
                          ObjectId("57b33489e5b9ce24f4910857"),
                          ObjectId("57b33491e5b9ce24f4910858")
                         ]
                     }
                 },
                 {$set{'isCurrentStatus':true}}, 
                 {multi:true}
              )

Query result

于 2016-08-18T16:11:38.217 回答