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 ...
随机推荐
- C++ 迭代器类别
1.output迭代器:一次一步,只能向前,流水线上放着一批空的盒子,每次向前移动一个,往盒子里面塞一个东西. 2.input迭代器:一次一步,只能向前,流水线上放着一批零件,每次向前移动一个,可以看 ...
- 2014 年10个最佳的PHP图像操作库--留着有用
Thomas Boutell 以及众多的开发者创造了以GD图形库闻名的一个图形软件库,用于动态的图形计算. GD提供了对于诸如C, Perl, Python, PHP, OCaml等等诸多编程语言的支 ...
- CodeForces 173E Camping Groups 离线线段树 树状数组
Camping Groups 题目连接: http://codeforces.com/problemset/problem/173/E Description A club wants to take ...
- 手把手教你使用 Imagepro plus - 宏操作【转】
Imagepro plus操作5 – 提高测量效率的必须技术-宏操作(续) 星期三, 七月 7th, 2010 | 图像分析 | hbchendl | 浏览:897 请先参阅:Imagepro plu ...
- 柯南君 :Oracle 分区技术 之 怎样支撑大数据操作?
前段时间.看了罗女士( 资深技术顾问 - Oracle 中国 顾问咨询部)关于<大批量数据处理技术的演讲>视频.感觉受益良多,结合多年的知识积累,柯南君给大家分享一下: 交流内容: 一.O ...
- JS的setTimeout函数第一个参数问题
setTimeout的第一个参数只能放一个无参的函数,更像放了一个函数指针在那里,如果要放带参数的话,就要拿个匿名函数包裹一下
- nexus-2.13.0-01.war
https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.13.0-01.war
- EF——一对一、一对多、多对多关系的配置和级联删除 04(转)
EF里一对一.一对多.多对多关系的配置和级联删除 本章节开始了解EF的各种关系.如果你对EF里实体间的各种关系还不是很熟悉,可以看看我的思路,能帮你更快的理解. I.实体间一对一的关系 添加一个P ...
- solr查询在solrconfig.xml中的配置
<requestHandler name="/select" class="solr.SearchHandler"> <lst name=&q ...
- nodejs的mysql模块学习(六)连接池的创建和使用
介绍 在 软件工程 , 连接池 是一个 高速缓存 的 数据库连接 维持,使得连接可以当需要将来向数据库请求重复使用. [ 来源请求 ] 连接池用于提高数据库上执行命令的性能. 打开并保持每个用户的数据 ...