|
jwelch, this is expected behavior caused by the 64bit double-precision floating-point numbers representation used in JavaScript, and you can observer the same behavior in V8:
> x = 9223372036854775807
|
9223372036854776000
|
> y = 9223372036854775203
|
9223372036854775000
|
By the time these numbers reach the server they've already been rounded by V8.
If you need to handle 64bit integers in the mongo shell please see the NumberLong() wrapper. Here's an example:
> db.test.drop()
|
>
|
> // Insert 2^63 - 2
|
> db.test.save({value: NumberLong("9223372036854775806")})
|
WriteResult({ "nInserted" : 1 })
|
> db.test.find()
|
{ "_id" : ObjectId("54d8d94906c26ec9753989d8"), "value" : NumberLong("9223372036854775806") }
|
>
|
> // Increment value
|
> db.test.update({}, {$inc : {value:NumberLong("1")}})
|
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
|
>
|
> // Search using $gt
|
> db.test.find({value: {$gt: NumberLong("9223372036854775806")}})
|
{ "_id" : ObjectId("54d8d94906c26ec9753989d8"), "value" : NumberLong("9223372036854775807") }
|
>
|
> // 2^63 - + 1 should trigger an error:
|
> db.test.update({}, {$inc : {value:NumberLong("1")}})
|
WriteResult({
|
"nMatched" : 0,
|
"nUpserted" : 0,
|
"nModified" : 0,
|
"writeError" : {
|
"code" : 16837,
|
"errmsg" : "Failed to apply $inc operations to current value ((NumberLong)9223372036854775807) for document {_id: ObjectId('54d8d94906c26ec9753989d8')}"
|
}
|
})
|
|