mongodb update matched doc fail

﹥>﹥吖頭↗ 提交于 2019-12-13 05:15:29

问题


There are 10000 docs import by mongoimport, the _id is 6 length random string

{_id:"xxxxxx","u":0,"t":0}

because mongoimport can not specifiy data type, so string like "123456" was imported as type int. So I manually delete originals and re-insert them as

db.xxx.insert({_id:"123456","u":0,"t":0})

Because the default type of 0 is Double, so I change them to int by:

db.xxx.update({},{$set:{u:NumberInt(0)}},false,true)
WriteResult({ "nMatched" : 100000, "nUpserted" : 0, "nModified" : 99994 })

Seems 6 docs change failed, I validate the change by:

> db.code.find({u:{$type:1}})
{ "_id" : "263798", "t" : 4, "u" : 0 }
{ "_id" : "375249", "t" : 7, "u" : 0 }
{ "_id" : "659472", "t" : 3, "u" : 0 }
{ "_id" : "672534", "t" : 3, "u" : 0 }
{ "_id" : "784392", "t" : 0, "u" : 0 }
{ "_id" : "875631", "t" : 0, "u" : 0 }

the update only modify docs which was imported by mongoimport but leave alone docs which I manually insert, why?


回答1:


It's not a failure but by design.

Under the Bulk Operations API, if you supply a value to update that matches an existing value of the document then it is not marked as modified and actually does not make any attempt to re-write the document.

Simple test:

db.junk.insert({ "a": 1 })
WriteResult({ "nInserted" : 1 })

db.junk.update({ "a": 1},{ "$set": { "a": 2 }})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

db.junk.update({ "a": 2 },{ "$set": { "a": 2 }})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })

db.junk.update({ "a": 2 },{ "$set": { "a": NumberInt(2) }})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

db.junk.update({ },{ "$set": { "a": NumberInt(2) }})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })

All operations in the MongoDB shell as of version 2.6 are actually using the Bulk Operations API. This is where you see the WriteResult which comes from that API as proof this is happening.

So the short case here is if you have "manually inserted" items that are of the correct type you are modifying to, then they do not get changed.



来源:https://stackoverflow.com/questions/27857427/mongodb-update-matched-doc-fail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!