【问题标题】:Mongo driver updateMany not updating all documentsMongodb驱动更新很多没有更新所有文档
【发布时间】:2016-08-26 03:16:49
【问题描述】:

针对 MongoDB 3.0.12 使用 Mongo Java 驱动程序版本 3.2.1。

调用MongoCollection.updateMany(Bson filter, Bson update) 返回的结果显示所有预期的文档都已修改,但实际上只有部分文档被更新。

我尝试了多个写入问题:JOURNLED、ACKNOWLEDGED 等

有什么想法吗?

这是配置文件结果:

{ "op" : "update", "ns" : "dev.timeSheet", "query" : { "lineItems.task" : ObjectId("53233e85e4b07f573f1d4466") }, "updateobj" : { "$set" : { "lineItems.$.task" : ObjectId("53233e85e4b07f573f1d446d") } }, "nscanned" : 0, "nscannedObjects" : 6733, "nMatched" : 248, "nModified" : 248, "fastmod" : true, "keyUpdates" : 0, "writeConflicts" : 0, "numYield" : 52, "locks" : { "Global" : { "acquireCount" : { "r" : NumberLong(53), "w" : NumberLong(53) } }, "MMAPV1Journal" : { "acquireCount" : { "w" : NumberLong(301) } }, "Database" : { "acquireCount" : { "w" : NumberLong(53) } }, "Collection" : { "acquireCount" : { "W" : NumberLong(53) } } }, "millis" : 50, "execStats" : {  }, "ts" : ISODate("2016-08-25T18:17:16.025Z"), "client" : "127.0.0.1", "allUsers" : [ ], "user" : "" }

更新:也出现在 MongoDB 3.2.9 中 直接访问调用:

db.timeSheet.find({'lineItems.task': ObjectId("53233e85e4b07f573f1d4466")}).count()
126
db.timeSheet.updateMany({'lineItems.task': ObjectId("53233e85e4b07f573f1d4466")}, {'$set': {'lineItems.$.task': ObjectId("53233e85e4b07f573f1d446d")}})
{ "acknowledged" : true, "matchedCount" : 126, "modifiedCount" : 126 }
db.timeSheet.find({'lineItems.task': ObjectId("53233e85e4b07f573f1d4466")}).count()
90

【问题讨论】:

  • 发布对 MongoDB 的实际调用,而不是抽象代码。否则真的不知道
  • 如果更新是指替换,那么不要使用$set 运算符。

标签: java mongodb


【解决方案1】:

这不是我预期的工作方式,因为它只是更新它为每个时间表找到的第一个 lineItem

https://docs.mongodb.com/manual/reference/operator/update/positional/#up.S

请记住,位置 $ 运算符充当更新查询文档的第一个匹配项的占位符。

目前不存在更新嵌入数组中所有项目的功能。

https://jira.mongodb.org/browse/SERVER-1243

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 2017-07-03
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    相关资源
    最近更新 更多