Вопрос

I'm using MongoDB 2.2.3 on 64bit Ubuntu 12.04 build. The output in my Mongo Shell is the following :

>db.clusters.findOne({'_id':-3118541015882674000})
{
    "_id" : NumberLong("-3118541015882673983"),
    "members" : [
        {
            "participationCoeff" : 1,
            "tweetID" : NumberLong("-3118541015882673983")
        },
        {
            "participationCoeff" : 0.6666666666666666,
            "tweetID" : NumberLong("-7489837299951056630")
        },
        {
            "participationCoeff" : 0.5,
            "tweetID" : NumberLong("-4808081224284120148")
        }
    ]
}

As you can see, the number given in matching condition is not the same as what is returned. The _id field doesn't have the same value.

Это было полезно?

Решение

What's going on here is that JavaScript cannot natively represent 64-bit integers like -3118541015882673983, so when converting that NumberLong value to a JS 64-bit float it loses precision and ends up as -3118541015882674000.

You can see this in the mongo shell:

> num = NumberLong("-3118541015882673983")
NumberLong("-3118541015882673983")
> num.toNumber()
-3118541015882674000

So in this case you're proving the _id in the query as a 64-bit float so mongo converts the numeric doc _ids to the same data type so that they can be compared and you get a match.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top