MongoDB Number字段将不会插入或更新为我输入的数字 [英] MongoDB Number Field will not insert or update with the number that I input
问题描述
这是我的问题:
db.Group6391102Bounds.insert({bound:"latest",id:138548488276343678,complete:false}) db.Group6391102Bounds.find()
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()
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()
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 }
如您所见,即使我插入了一个特定的ID,mongoDB也会添加一个不同的ID. 我不知道为什么会这样.任何帮助将不胜感激.感恩节快乐!
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!
推荐答案
对不起,我一开始不理解您的问题,因此提供了错误的答案(感谢Cababunga指出了这一点).所以这是正确的.
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支持不同的数据类型.并且当您输入数据时,它会尝试猜测您的数据类型.因此,您输入您的大号码: 138548488276343678 .请注意,它大于然后是2 ^ 31-1,这是32位整数的最大值.因此将其视为浮点数,并且由于浮点数未精确存储,因此对其进行了一些修改.这就是为什么您存储的数字几乎相同,但相差一点点的原因(此差异小于8).但是您想精确地存储此数字,并且mongo支持64位整数(适合您的整数).
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).
因此,您需要指定要存储为 64位整数.您可以通过以下方式进行操作:
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
})
此后,您可以检索文档db.a.find()
,它将是正确的.请注意,许多驱动程序都有类似的问题,因此必须明确告知您要将它们保存为64位整数.
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:
如果您没有为要创建的文档指定_id
,则mongodb会自行创建_id
字段.您可以在_id
此处和
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.
如果您希望将自己的字段用作_id
,则应该编写_id : 138548488276343678
,而不是编写id:138548488276343678
.
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.还因为我看到您使用的是很大的数字,所以请记住,mongodb中的整数存储为
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)
这篇关于MongoDB Number字段将不会插入或更新为我输入的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!