mongo
Shellmongo
Shell中的数据类型¶On this page本页内容
Note
The following document pertains to the 以下文档涉及MongoDB服务器下载中包含的mongo
shell included in the MongoDB Server Download. mongo
shell。For information on the new MongoDB Shell, 有关新MongoDB外壳mongosh
, refer to the mongosh Documentation.mongosh
的信息,请参阅mongosh文档。
To understand the differences between the two shells, see Comparison of the mongo Shell and mongosh.要了解这两种外壳之间的差异,请参阅mongo
外壳和mongosh
外壳的比较。
MongoDB BSON provides support for additional data types than JSON. MongoDB BSON提供了对JSON以外的其他数据类型的支持。Drivers provide native support for these data types in host languages and the 驱动程序在主机语言中为这些数据类型提供了本机支持,mongo
shell also provides several helper classes to support the use of these data types in the mongo
JavaScript shell. mongo
shell还提供了几个帮助器类来支持在mongo
JavaScript shell中使用这些数据类型。See the Extended JSON reference for additional information.有关更多信息,请参阅扩展JSON参考。
The mongo
shell provides various methods to return the date, either as a string or as a Date
object:mongo
shell提供了各种方法来返回日期,可以是字符串,也可以是日期对象:
Date()
new Date()
Date
object using the ISODate()
wrapper.ISODate()
包装返回Date
对象。ISODate()
Date
object using the ISODate()
wrapper.ISODate()
包装返回Date
对象。Internally, Date objects are stored as a signed 64-bit integer representing the number of milliseconds since the Unix epoch (Jan 1, 1970).在内部,Date
对象存储为有符号64位整数,表示自Unix纪元(1970年1月1日)以来的毫秒数。
Not all database operations and drivers support the full 64-bit range. 并非所有数据库操作和驱动程序都支持完整的64位范围。You may safely work with dates with years within the inclusive range 您可以安全地使用年数在0
through 9999
.0
到9999
之间的日期。
To return the date as a string, use the 要以字符串形式返回日期,请使用Date()
method, as in the following example:date()
方法,如以下示例所示:
To print the value of the variable, type the variable name in the shell, as in the following:要打印变量的值,请在shell中键入变量名称,如下所示:
The result is the value of 结果是myDateString
:myDateString
的值:
To verify the type, use the 要验证类型,请使用typeof
operator, as in the following:typeof
运算符,如下所示:
The operation returns 该操作返回string
.string
。
Date
¶The mongo
shell wraps objects of Date
type with the ISODate
helper; however, the objects remain of type Date
.mongo
shell使用ISODate
辅助对象包装日期类型的对象;但是,对象仍然是Date
类型。
The following example uses both the 下面的示例使用new Date()
constructor and the ISODate()
constructor to return Date
objects.new Date()
构造函数和ISODate()
构造函数返回日期对象。
You can use the 您还可以将新运算符与new
operator with the ISODate()
constructor as well.ISODate()
构造函数一起使用。
To print the value of the variable, type the variable name in the shell, as in the following:要打印变量的值,请在shell中键入变量名称,如下所示:
The result is the 结果是包装在Date
value of myDate
wrapped in the ISODate()
helper:ISODate()
帮助程序中的myDate
的日期值:
To verify the type, use the 要验证类型,请使用instanceof
operator, as in the following:instanceof
运算符,如下所示:
The operation returns 这两个操作都返回true
for both.true
。
The mongo
shell provides the ObjectId()
wrapper class around the ObjectId data type. mongo
shell围绕ObjectId数据类型提供ObjectId()
包装类。To generate a new ObjectId, use the following operation in the 要生成新的ObjectId,请在mongo
shell:mongo
shell中使用以下操作:
See
The 默认情况下,mongo
shell treats all numbers as floating-point values by default. mongo
shell将所有数字视为浮点值。The mongo
shell provides the NumberLong()
wrapper to handle 64-bit integers.mongo
shell提供NumberLong()
包装来处理64位整数。
The NumberLong()
wrapper accepts the long as a string:NumberLong()
包装器接受长字符串:
The following examples use the 以下示例使用NumberLong()
wrapper to write to the collection:NumberLong()
包装器写入集合:
Retrieve the document to verify:检索要验证的文档:
In the returned document, the 在返回的文档中,calc
field contains a NumberLong
object:calc
字段包含一个NumberLong
对象:
If you use the 如果使用$inc
to increment the value of a field that contains a NumberLong
object by a float, the data type changes to a floating point value, as in the following example:$inc
将包含NumberLong
对象的字段的值增加一个浮点,则数据类型将更改为浮点值,如下例所示:
$inc
to increment the calc
field by 5
, which the mongo
shell treats as a float:$inc
将calc
字段增加5,mongo
shell将其视为浮点:
In the updated document, the 在更新的文档中,calc
field contains a floating point value:calc
字段包含一个浮点值:
Note
Although the 尽管NumberLong()
constructor accepts integer
values from the mongo
shell (i.e. without quotes), this is not recommended. NumberLong()
构造函数接受来自mongo
shell的integer
(即不带引号),但不建议这样做。Specifying an integer value larger than JavaScript’s defined 指定大于JavaScript定义的Number.MAX_SAFE_INTEGER
(which is the number 2^53 - 1
) may lead to unexpected behavior.Number.MAX_SAFE_integer
(即数字2^53-1
)的整数值可能会导致意外行为。
The 默认情况下,mongo
shell treats all numbers as floating-point values by default. mongo
shell将所有数字视为浮点值。The mongo
shell provides the NumberInt()
constructor to explicitly specify 32-bit integers.mongo
shell提供numberrint()
构造函数来显式指定32位整数。
New in version 3.4.版本3.4中的新功能。
The 默认情况下,mongo
shell treats all numbers as 64-bit floating-point double
values by default. mongo
shell将所有数字视为64位浮点双精度值。The mongo
shell provides the NumberDecimal()
constructor to explicitly specify 128-bit decimal-based floating-point values capable of emulating decimal rounding with exact precision. mongo
shell提供NumberDecimal()
构造函数,以显式指定基于128位十进制的浮点值,该浮点值能够模拟精确的十进制舍入。This functionality is intended for applications that handle monetary data, such as financial, tax, and scientific computations.此功能适用于处理货币数据的应用程序,如金融、税务和科学计算。
The decimal
BSON type uses the IEEE 754 decimal128 floating-point numbering format which supports 34 decimal digits (i.e. significant digits) and an exponent range of −6143 to +6144.decimal
BSON类型使用IEEE 754 decimal128浮点编号格式,支持34位十进制数字(即有效数字),指数范围为6143至+6144。
The NumberDecimal()
constructor accepts the decimal
value as a string:NumberDecimal()
构造函数接受十进制值作为字符串:
The value is stored in the database as follows:该值存储在数据库中,如下所示:
The NumberDecimal()
constructor also accepts double
values from the mongo
shell (i.e. without quotes), although this is not recommended due to the risk of losing precision. NumberDecimal()
构造函数也接受来自mongo shell的双值(即不带引号),尽管不建议这样做,因为这样可能会丢失精度。The constructor creates a binary-based 构造函数创建基于十进制的参数的基于二进制的双精度表示(可能会丢失精度),然后将该值转换为精度为15位的十进制值。double
precision representation of the decimal-based parameter (potentially losing precision), then converts that value to a decimal
value with a precision of 15 digits. The following example passes the value implicitly as a 以下示例以double
and shows how it is created with a precision of 15 digits:double
隐式传递值,并显示如何创建精度为15位的值:
The value is stored in the database as follows:该值存储在数据库中,如下所示:
The following example passes the value implicitly as a 以下示例以double
and shows how a loss of precision can occur:double
隐式传递该值,并显示了如何发生精度损失:
The value is stored in the database as follows:该值存储在数据库中,如下所示:
Note
To use the 要在MongoDB驱动程序中使用decimal
data type with a MongoDB driver, be sure to use a driver version that supports it.decimal
数据类型,请确保使用支持它的驱动程序版本。
Values of the decimal
type are compared and sorted with other numeric types based on their actual numeric value. decimal
类型的值将根据其实际数值与其他数值类型进行比较和排序。Numeric values of the binary-based 基于二进制的双精度类型的数值通常具有基于十进制值的近似表示形式,并且可能不完全等于其十进制表示形式,因此在检查十进制值的相等性时,请使用double
type generally have approximate representations of decimal-based values and may not be exactly equal to their decimal
representations, so use the NumberDecimal()
constructor when checking the equality of decimal
values. NumberDecimal()
构造函数。Consider the following examples with the following documents in the 请细看下面的示例,带有以下文档,形式为numbers
collection:numbers
集合:
When the queries from the table below are plugged into the 将下表中的查询插入db.numbers.find(<query>)
method, the following results are returned:db.numbers.find(<query>)
方法时,将返回以下结果:
{ “val”: 9.99 } | { “_id”: 2, “val”: 9.99, “description”: “Double” } |
{ “val”: NumberDecimal( “9.99” ) } | { “_id”: 1, “val”: NumberDecimal( “9.99” ), “description”: “Decimal” } |
{ val: 10 } | { “_id”: 3, “val”: 10, “description”: “Double” }
{ “_id”: 4, “val”: NumberLong(10), “description”: “Long” }
{ “_id”: 5, “val”: NumberDecimal( “10.0” ), “description”: “Decimal” } |
{ val: NumberDecimal( “10” ) } | { “_id”: 3, “val”: 10, “description”: “Double” }
{ “_id”: 4, “val”: NumberLong(10), “description”: “Long” }
{ “_id”: 5, “val”: NumberDecimal( “10.0” ), “description”: “Decimal” } |
The first query, 第一个查询{ "val": 9.99 }
, implicitly searches for the double
representation of 9.99
which is not equal to the decimal
representation of the value.{ "val": 9.99 }
隐式搜索9.99
的双精度表示,它不等于值的十进制表示。
The NumberDecimal()
constructor is used to query for the document with the decimal
representation of 9.99
. NumberDecimal()
构造函数用于查询十进制表示为9.99的文档。Values of the 排除双精度类型的值,因为它们与十进制表示法double
type are excluded because they do not match the exact value of the decimal
representation of 9.99
.9.99
的精确值不匹配。
Matching values of all numeric types are returned when querying for whole numbers. 查询整数时返回所有数字类型的匹配值。For example, querying for a 例如,查询double
representation of 10
will include a decimal
representation of 10.0
in the results and vice versa.10
的双精度表示将在结果中包含10.0
的十进制表示,反之亦然。
mongo
Shellmongo
Shell中的类型¶To determine the type of fields, the 为了确定字段的类型,mongo
shell provides the instanceof
and typeof
operators.mongo
shell提供instanceof
和typeof操作符。
instanceof
¶instanceof
returns a boolean to test if a value is an instance of some type.instanceof
返回一个布尔值,以测试值是否为某种类型的实例。
For example, the following operation tests whether the 例如,以下操作测试_id
field is an instance of type ObjectId
:_id
字段是否为ObjectId
类型的实例:
The operation returns 该操作返回true
.true
。
typeof
¶typeof
returns the type of a field.返回字段的类型。
For example, the following operation returns the type of the 例如,以下操作返回_id
field:_id
字段的类型:
In this case 在这种情况下,typeof
will return the more generic object
type rather than ObjectId
type.typeof
将返回更通用的object
类型,而不是ObjectId
类型。