Hi @Nikita_Kozelko
This will be the language or interpreter losing precision of the number before it is inserted to mongodb.
For example with mongosh (a javascript REPL environment) the default number type is a double, once this number exceeds the MAX_SAFE_INTEGER value precision is lost and duplicate numbers are a result of that. Instead native(or bson library provided) types need to be used.
// javascript default number
new Array(10).fill().map((x,i)=>{return {_id: Number.MAX_SAFE_INTEGER + i}})
[
{ _id: 9007199254740991 },
{ _id: 9007199254740992 },
{ _id: 9007199254740992 },
{ _id: 9007199254740994 },
{ _id: 9007199254740996 },
{ _id: 9007199254740996 },
{ _id: 9007199254740996 },
{ _id: 9007199254740998 },
{ _id: 9007199254741000 },
{ _id: 9007199254741000 }
]
//javascript Long
new Array(10).fill().map((x,i)=>{return {_id: Long(Number.MAX_SAFE_INTEGER.toString()).add(i)}})
[
{ _id: Long('9007199254740991') },
{ _id: Long('9007199254740992') },
{ _id: Long('9007199254740993') },
{ _id: Long('9007199254740994') },
{ _id: Long('9007199254740995') },
{ _id: Long('9007199254740996') },
{ _id: Long('9007199254740997') },
{ _id: Long('9007199254740998') },
{ _id: Long('9007199254740999') },
{ _id: Long('9007199254741000') }
]
This example show a sequence inserted by python starting at the max signed int32 value and the result of reading this in both python and mongosh(javascript).
#!/usr/bin/env python3
import os
from pprint import pprint
import pymongo
maxint32=0xFFFFFFFF >> 1
client = pymongo.MongoClient(os.getenv('MONGODB_URI'))
client.test.foo.drop()
for i in range(10):
client.test.foo.insert_one({ '_id': maxint32 + i })
pprint([ _ for _ in client.test.foo.find() ])
> python3 test
[{'_id': 2147483647},
{'_id': 2147483648},
{'_id': 2147483649},
{'_id': 2147483650},
{'_id': 2147483651},
{'_id': 2147483652},
{'_id': 2147483653},
{'_id': 2147483654},
{'_id': 2147483655},
{'_id': 2147483656}]
Notice the change from ‘Number’ to Long.
mongosh ${MONGODB_URI} --eval 'db.foo.find()'
[
{ _id: 2147483647 },
{ _id: Long('2147483648') },
{ _id: Long('2147483649') },
{ _id: Long('2147483650') },
{ _id: Long('2147483651') },
{ _id: Long('2147483652') },
{ _id: Long('2147483653') },
{ _id: Long('2147483654') },
{ _id: Long('2147483655') },
{ _id: Long('2147483656') }
]