如何在未设置的更新操作中正确使用位置运算符$ []?

时间:2018-12-20 18:26:32

标签: mongodb unset positional-operator

根据mongodbs文档,位置运算符$ []应该对数组的所有元素进行运算,但不适用于$ unset。它修改了0个文档

我需要以编程方式执行此操作,但只能通过手动展开数组来执行此操作。

aireclaimRs:PRIMARY> db.cases.updateMany({},{$unset: {"casoOriginal.people.$[].contract":""}},{multi:true})
{ "acknowledged" : true, "matchedCount" : 2502, "modifiedCount" : 0 }

aireclaimRs:PRIMARY> db.cases.updateMany({},{$unset: {"casoOriginal.people.0.contract":""}},{multi:true})
{ "acknowledged" : true, "matchedCount" : 2502, "modifiedCount" : 1633 }
aireclaimRs:PRIMARY> db.cases.updateMany({},{$unset: {"casoOriginal.people.1.contract":""}},{multi:true})
{ "acknowledged" : true, "matchedCount" : 2502, "modifiedCount" : 517 }
aireclaimRs:PRIMARY> db.cases.updateMany({},{$unset: {"casoOriginal.people.2.contract":""}},{multi:true})
{ "acknowledged" : true, "matchedCount" : 2502, "modifiedCount" : 180 }
aireclaimRs:PRIMARY> db.cases.updateMany({},{$unset: {"casoOriginal.people.3.contract":""}},{multi:true})
{ "acknowledged" : true, "matchedCount" : 2502, "modifiedCount" : 87 }
aireclaimRs:PRIMARY> db.cases.updateMany({},{$unset: {"casoOriginal.people.4.contract":""}},{multi:true})
{ "acknowledged" : true, "matchedCount" : 2502, "modifiedCount" : 23 }
aireclaimRs:PRIMARY> db.cases.updateMany({},{$unset: {"casoOriginal.people.5.contract":""}},{multi:true})
{ "acknowledged" : true, "matchedCount" : 2502, "modifiedCount" : 10 }
aireclaimRs:PRIMARY> db.cases.updateMany({},{$unset: {"casoOriginal.people.6.contract":""}},{multi:true})
{ "acknowledged" : true, "matchedCount" : 2502, "modifiedCount" : 3 }
aireclaimRs:PRIMARY> db.cases.updateMany({},{$unset: {"casoOriginal.people.7.contract":""}},{multi:true})
{ "acknowledged" : true, "matchedCount" : 2502, "modifiedCount" : 1 }
aireclaimRs:PRIMARY> db.cases.updateMany({},{$unset: {"casoOriginal.people.8.contract":""}},{multi:true})
{ "acknowledged" : true, "matchedCount" : 2502, "modifiedCount" : 1 }
aireclaimRs:PRIMARY> db.cases.updateMany({},{$unset: {"casoOriginal.people.9.contract":""}},{multi:true})
{ "acknowledged" : true, "matchedCount" : 2502, "modifiedCount" : 0 }

预期:第一个操作应与以下操作的合并操作相同。

结果:第一个操作会修改0个文档,随后的展开操作会正确地修改文档。

0 个答案:

没有答案