NumberLong 和简单 Integer 之间的 MongoDB 区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17185220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:17:53  来源:igfitidea点击:

MongoDB differences between NumberLong and simple Integer?

mongodb

提问by Francisco Costa

What are the main differences (size, speed, etc) between the datatypes double, NumberLong, NumberInt or a simple Integer in MongoDB?

MongoDB 中的数据类型 double、NumberLong、NumberInt 或简单的 Integer 之间的主要区别(大小、速度等)是什么?

If I want to save a small fixed number (something between 0 and 1000) which data type should I use?

如果我想保存一个小的固定数字(介于 0 和 1000 之间),我应该使用哪种数据类型?

回答by Jayram

NumberInt

整数

By default, the mongo shell treats all numbers as floating-point values. The mongo shell provides the NumberInt()constructor to explicitly specify 32-bit integers.

默认情况下,mongo shell 将所有数字视为浮点值。mongo shell 提供NumberInt()构造函数来显式指定 32 位整数。

NumberLong

号码长

By default, the mongo shell treats all numbers as floating-point values. The mongo shell provides the NumberLong()class to handle 64-bit integers.

默认情况下,mongo shell 将所有数字视为浮点值。mongo shell 提供了NumberLong()处理 64 位整数的类。

The NumberLong()constructor accepts the long as a string:

NumberLong()构造函数接受的,只要一个字符串:

NumberLong("2090845886852")

Source: http://docs.mongodb.org/manual/core/shell-types/

来源:http: //docs.mongodb.org/manual/core/shell-types/

回答by Florian Winter

NumberLongand NumberIntare not data types in MongoDB but JavaScript functions in the MongoDB shell.

NumberLong并且NumberInt不是 MongoDB 中的数据类型,而是 MongoDB shell 中的 JavaScript 函数。

Data types in MongoDB are defined in the BSON specification: http://bsonspec.org/spec.html

MongoDB 中的数据类型在 BSON 规范中定义:http: //bsonspec.org/spec.html

Numbers are stored as type 0x01 (floating point), type 0x10 (32-bit integer) or type 0x12 (64-bit integer).

数字存储为类型 0x01(浮点)、类型 0x10(32 位整数)或类型 0x12(64 位整数)。

If you insert or update a document in the MongoDB shell, then NumberLongcreates a 64-bit integer, NumberIntcreates a 32-bit integer, and a regular JavaScript number creates a floating-point value. This is because there are no integers in JavaScript, only floating-point numbers.

如果您在 MongoDB shell 中插入或更新文档,则NumberLong创建一个 64 位整数,NumberInt创建一个 32 位整数,一个常规 JavaScript 数字创建一个浮点值。这是因为 JavaScript 中没有整数,只有浮点数。

Output in the MongoDB shell shows floating-point numbers and 32-bit integers as JavaScript numbers, whereas 64-bit integers are shown as calls to NumberLong:

MongoDB shell 中的输出将浮点数和 32 位整数显示为 JavaScript 数字,而 64 位整数显示为对 的调用NumberLong

> db.inttest.insert({f: 1234, i: NumberInt("1234"), l: NumberLong("1234")})
> db.inttest.findOne()
{
        "_id" : ObjectId("5396fc0db8e0b3e2dedb59b0"),
        "f" : 1234,
        "i" : 1234,
        "l" : NumberLong(1234)
}

Different MongoDB drivers provide different methods of inserting different types of numbers. For example, the C++ driver creates a 64-bit integer if you append a long longvalue to a BSONObjectBuilder.

不同的 MongoDB 驱动程序提供了不同的方法来插入不同类型的数字。例如,如果将long long值附加到 BSONObjectBuilder ,C++ 驱动程序会创建一个 64 位整数。

Queries match whenever the numbers are equal. In the above example, the queries

只要数字相等,查询就会匹配。在上面的例子中,查询

> db.inttest.find({i:1234})
> db.inttest.find({l:1234})
> db.inttest.find({f:1234})
> db.inttest.find({i:NumberLong("1234")})
> db.inttest.find({l:NumberLong("1234")})
> db.inttest.find({f:NumberLong("1234")})

all match the inserted document.

所有匹配插入的文档。