简体   繁体   中英

How does MongoDB Compass deal with int64?

I have read the official documentation about int64 and I need use NumberLong wrapper the int64. But I find there is some special values could be used without NumberLong: 在此处输入图片说明

In my image, I think the MongoDB Compass will treat 1128505640310804481 as double just like Javascript and use the round 1128505640310804500(this is what I get from Javascript). The data in DB is shown in int64 so I think the 1128505640310804481 is stored correctly as int64. Since 1128505640310804500 is not equal to 1128505640310804481, I think I should find no data matched my filter, but MongoDB Compass give me the result.

So my question is: when I enter int64 in MongoDB Compass Filter like the picture, how does it deal with the int64 and why it could match the correct int64 data stored in DB?

when I enter int64 in MongoDB Compass Filter like the picture, how does it deal with the int64 and why it could match the correct int64 data stored in DB?

To start with, MongoDB can store 64-bit integer value because data are stored as BSON (binary serialisation format). This solves the issue in the server. See also BSON Types .

Now, for MongoDB Compass it is able to identify the type of number (int32, int64, or Double) by auto-casting. It detects the value in the editor, when an int32 is edited to a value over 32 bits AND the value passes the +/- of Number.isSafeInteger then it casts to int64 .

Part of MongoDB Compass that does the type checking is actually has been open-sourced. See the type checker code: mongodb-js/hadron-type-checker/blob/master/src/type-checker.js . The NPM package is hadron-type-checker .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM