MongoDB Number Field will not insert or update with the number that I input

ぃ、小莉子 提交于 2019-12-10 23:16:52

问题


this is my problem:

db.Group6391102Bounds.insert({ bound:"latest",id:138548488276343678,complete:false}) db.Group6391102Bounds.find()

   { "_id" : ObjectId("5297d9e5ef9f659b82271617"), "bound" : "earliest", "id" : 138548488276343680, "complete" : false }
   { "_id" : ObjectId("5297dc28b2d702ea878b540d"), "bound" : "latest", "id" : 138548488276343680, "complete" : false }

db.Group6391102Bounds.insert({ bound:"middle",id:138548488276343678,complete:false}) db.Group6391102Bounds.find()

   { "_id" : ObjectId("5297d9e5ef9f659b82271617"), "bound" : "earliest", "id" : 138548488276343680, "complete" : false }
   { "_id" : ObjectId("5297dc28b2d702ea878b540d"), "bound" : "latest", "id" : 138548488276343680, "complete" : false }
   { "_id" : ObjectId("5297dc3cb2d702ea878b540e"), "bound" : "middle", "id" : 138548488276343680, "complete" : false }

db.Group6391102Bounds.insert({ bound:"middle",name:138548488276343678,complete:false}) db.Group6391102Bounds.find()

   { "_id" : ObjectId("5297d9e5ef9f659b82271617"), "bound" : "earliest", "id" : 138548488276343680, "complete" : false }
   { "_id" : ObjectId("5297dc28b2d702ea878b540d"), "bound" : "latest", "id" : 138548488276343680, "complete" : false }
   { "_id" : ObjectId("5297dc3cb2d702ea878b540e"), "bound" : "middle", "id" : 138548488276343680, "complete" : false }
   { "_id" : ObjectId("5297dc91b2d702ea878b540f"), "bound" : "middle", "name" : 138548488276343680, "complete" : false }

As you can see, even though I insert a specific ID, mongoDB will add a different ID. I have no Idea why this is happening. Any help would be greatly appreciated. Happy Thanksgiving!


回答1:


Sorry, I did not understand your question in the beginning and therefore provided wrong answer (thanks cababunga for pointing this out). So here is a correct one.

Mongoshell supports different data types. And it tries to guess your datatype when you enter it. So you enter your big number: 138548488276343678. Note that it is bigger then then 2^31-1 which is the maximum for 32-bit integer. So it treats it as a float and because floats are not stored precisely, it modifies it a little bit. This is why your stored number is almost the same, but differs by a little bit (this difference will be less then 8). But you want to store this number precise and mongo support 64-bit integer (which fits your integer).

So you need to specify that you want to store it as 64bit integer. You can do this in the following way:

db.a.insert({
  bound:"latest",
  id: NumberLong("138548488276343678"),  // Note these "". I was not using them and the number was not stored correctly
  complete:false
})

After this you can retrieve your document db.a.find() and it will be correct. Note that a lot of drivers have similar problems and therefore you have to explicitly tell that you are going to save them as 64bit integer.

And here is my wrong attempt. If you thing that it should not be here, please modify my question:

If you are not specifying _id for the document you are creating, mongodb creates _id field by itselft. You can read a little bit more about _id here and in official documentation.

If you have your own field, which you would like to be used as _id, instead of writing id:138548488276343678 you should write _id : 138548488276343678.

P.S. also because I see that you are using quite big numbers, keep in mind that integers in mongodb as stored as 64-bit integers (which means that it is between -2^63 to 2^63 - 1)



来源:https://stackoverflow.com/questions/20276622/mongodb-number-field-will-not-insert-or-update-with-the-number-that-i-input

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