MongoDB - The mongo Shell, Data Types in the mongo Shell
MongoDB BSON provides support for additional data types than 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. See the Extended JSON reference for additional information.
Types
Date
The mongo shell provides various methods to return the date, either as a string or as a Date object:
- Date() method which returns the current date as a string.
- new Date() constructor which returns a Date object using the ISODate() wrapper.
- ISODate() constructor which returns a Date object using the ISODate() wrapper.
Internally, Date objects are stored as a 64 bit integer representing the number of milliseconds since the Unix epoch (Jan 1, 1970), which results in a representable date range of about 290 millions years into the past and future.
Return Date as a String
To return the date as a string, use the Date() method, as in the following example:
var myDateString = Date();
To print the value of the variable, type the variable name in the shell, as in the following:
myDateString
The result is the value of myDateString:
Thu Dec 01 2016 00:44:26 GMT+0800 (CST)
To verify the type, use the typeof operator, as in the following:
typeof myDateString
The operation returns string.
Return Date
The mongo shell wraps objects of Date type with the ISODate helper; however, the objects remain of type Date.
The following example uses both the new Date() constructor and the ISODate() constructor to return Date objects.
var myDate = new Date();
var myDateInitUsingISODateWrapper = ISODate();
You can use the new operator with the ISODate() constructor as well.
To print the value of the variable, type the variable name in the shell, as in the following:
myDate
The result is the Date value of myDate wrapped in the ISODate() helper:
ISODate("2016-11-30T16:48:47.527Z")
To verify the type, use the instanceof operator, as in the following:
myDate instanceof Date
myDateInitUsingISODateWrapper instanceof Date
The operation returns true for both.
ObjectId
The mongo shell provides the ObjectId() wrapper class around the ObjectId data type. To generate a new ObjectId, use the following operation in the mongo shell:
new ObjectId
SEE: ObjectId
NumberLong
By default, the mongo shell treats all numbers as floating-point values. The mongo shell provides theNumberLong() wrapper to handle 64-bit integers.
The NumberLong() wrapper accepts the long as a string:
NumberLong("2090845886852")
The following examples use the NumberLong() wrapper to write to the collection:
db.collection.insert( { _id: 10, calc: NumberLong("2090845886852") } )
db.collection.update( { _id: 10 }, { $set: { calc: NumberLong("2555555000000") } } )
db.collection.update( { _id: 10 }, { $inc: { calc: NumberLong(5) } } )
Retrieve the document to verify:
db.collection.findOne( { _id: 10 } )
In the returned document, the calc field contains a NumberLong object:
{ "_id" : 10, "calc" : NumberLong("2555555000005") }
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:
- Use $inc to increment the calc field by 5, which the mongo shell treats as a float:
db.collection.update( { _id: 10 }, { $inc: { calc: 5 } } )
- Retrieve the updated document:
db.collection.findOne( { _id: 10 } )
In the updated document, the calc field contains a floating point value:
{ "_id" : 10, "calc" : 2555555000010 }
NumberInt
By default, the mongo shell treats all numbers as floating-point values. The mongo shell provides theNumberInt() constructor to explicitly specify 32-bit integers.
Check Types in the mongo Shell
To determine the type of fields, the mongo shell provides the instanceof and typeof operators.
instanceof
instanceof returns a boolean to test if a value is an instance of some type.
For example, the following operation tests whether the _id field is an instance of type ObjectId:
mydoc._id instanceof ObjectId
The operation returns true.
typeof
typeof returns the type of a field.
For example, the following operation returns the type of the _id field:
typeof mydoc._id
In this case typeof will return the more generic object type rather than ObjectId type.
MongoDB - The mongo Shell, Data Types in the mongo Shell的更多相关文章
- 4.Data Types in the mongo Shell-官方文档摘录
总结: 1.MongoDB 的BSON格式支持额外的数据类型 2 Date 对象内部存储64位字节存整数,存储使用NumberLong()这个类来存,使用NumberInt()存32位整数,128位十 ...
- MongoDB - The mongo Shell, Write Scripts for the mongo Shell
You can write scripts for the mongo shell in JavaScript that manipulate data in MongoDB or perform a ...
- C and SQL data types for ODBC and CLI
C and SQL data types for ODBC and CLI This topic lists the C and SQL data types for ODBC and CLI a ...
- allow zero datetime=true导致datetime转换失败:MySql.Data.Types.MySqlDateTime”的对象无法转换为类型“System.Nullable`1[System.DateTime]
allow zero datetime=true导致datetime转换失败:MySql.Data.Types.MySqlDateTime”的对象无法转换为类型“System.Nullable`1[S ...
- "SQL Server does not handle comparison of NText, Text, Xml, or Image data types."
"SQL Server does not handle comparison of NText, Text, Xml, or Image data types." sql2000 ...
- ExtJS笔记 Ext.data.Types
This is a static class containing the system-supplied data types which may be given to a Field. Type ...
- Entity Framework Code First (七)空间数据类型 Spatial Data Types
声明:本文针对 EF5+, Visual Studio 2012+ 空间数据类型(Spatial Data Types)是在 EF5 中引入的,空间数据类型表现有两种: Geography (地理学上 ...
- Core Java Volume I — 3.3. Data Types
3.3. Data TypesJava is a strongly typed language(强类型语音). This means that every variable must have a ...
- Delphi Data Types
http://docwiki.embarcadero.com/RADStudio/XE6/en/Delphi_Data_Types Integer Data Types Type Descriptio ...
随机推荐
- 关于Excel导入的HDR=YES; IMEX=1详解
于Excel导入的HDR=YES; IMEX=1详解2011年12月27日 星期二 11:17 参数HDR的值:HDR=Yes,这代表第一行是标题,不做为数据使用 ,如果用HDR=NO,则表示第一行不 ...
- 以Outlook样式分组和排列数据项
转载:http://www.cnblogs.com/peterzb/archive/2009/05/29/1491781.html OutlookGrid:以Outlook样式分组和排列数据项 (这里 ...
- C++ Interview - using new and delete to alloc and free memory
1. dynamic create object and initialization int *pi = new int; // pi points to an uninitialized int ...
- Codeforces Round #188 (Div. 2) B. Strings of Power 水题
B. Strings of Power Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/318/p ...
- .net+mssql制作抽奖程序思路及源码
近期一直在研究数据库,刚好有个项目要做抽奖程序,恩,拿来练练手吧. 抽奖程序: 思路整理,无非就是点一个按钮,然后一个图片旋转一会就出来个结果就行了,可这个程序的要求不是这样的,是需要从数据库中随机抽 ...
- 【开发实例】C#调用SAPI实现语音合成的两种方法
我们都知道现在的语音合成TTS是可以通过微软的SAPI实现的,好处我就不多说了,方便而已,因为在微软的操作系统里面就自带了这个玩意,主要的方式有两种: 1.使用COM组件技术,不管是C++,C#,D ...
- 最长公共子序列(LCS问题)
先简单介绍下什么是最长公共子序列问题,其实问题很直白,假设两个序列X,Y,X的值是ACBDDCB,Y的值是BBDC,那么XY的最长公共子序列就是BDC.这里解决的问题就是需要一种算法可以快速的计算出这 ...
- [ES6] 13. Using the ES6 spread operator ...
The spread operator (...) allows you to "explode" an array into its individual elements. S ...
- kmp算法简明教程
在字符串s中寻找模式串p的位置,这是一个字符串匹配问题. 举例说明: i = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 s = a b a a c a b a a a b a a ...
- Docker 选项和命令
选项 -D=true|false 使用 debug 模式.默认为 false. -H, --host=[unix:///var/run/docker.sock]: tcp://[host:port]来 ...