JSON can only represent a subset of the types supported by BSON. To preserve type information, MongoDB adds the following extensions to the JSON format:

  • Strict mode. Strict mode representations of BSON types conform to the JSON RFC. Any JSON parser can parse these strict mode representations as key/value pairs; however, only the MongoDB internal JSON parser recognizes the type information conveyed by the format.
  • mongo Shell mode. The MongoDB internal JSON parser and the mongo shell can parse this mode.

The representation used for the various data types depends on the context in which the JSON is parsed.

Parsers and Supported Format

Input in Strict Mode

The following can parse representations in strict mode with recognition of the type information.

Other JSON parsers, including mongo shell and db.eval(), can parse strict mode representations as key/value pairs, but without recognition of the type information.

Input in mongo Shell Mode

The following can parse representations in mongo shell mode with recognition of the type information.

Output in Strict mode

mongoexport and REST and HTTP Interfaces output data in Strict mode.

Output in mongo Shell Mode

bsondump outputs in mongo Shell mode.

BSON Data Types and Associated Representations

The following presents the BSON data types and the associated representations in Strict mode and mongoShell mode.

Binary

data_binary

 Strict Mode  mongo Shell Mode
{ "$binary": "<bindata>", "$type": "<t>" }  BinData ( <t>, <bindata> )
  • <bindata> is the base64 representation of a binary string.
  • <t> is a representation of a single byte indicating the data type. In Strict mode it is a hexadecimal string, and in Shell mode it is an integer. See the extended bson documentation.

Date

data_date

Strict Mode mongo Shell Mode
 { "$date": "<date>" }   new Date ( <date> ) 

In Strict mode<date> is an ISO-8601 date format with a mandatory time zone field following the template YYYY-MM-DDTHH:mm:ss.mmm<+/-Offset>.

The MongoDB JSON parser currently does not support loading ISO-8601 strings representing dates prior to the Unix epoch. When formatting pre-epoch dates and dates past what your system’s time_ttype can hold, the following format is used:

{ "$date" : { "$numberLong" : "<dateAsMilliseconds>" } }

In Shell mode<date> is the JSON representation of a 64-bit signed integer giving the number of milliseconds since epoch UTC.

Timestamp

data_timestamp

Strict Mode mongo Shell Mode
{ "$timestamp": { "t": <t>, "i": <i> } } Timestamp( <t>, <i> )
  • <t> is the JSON representation of a 32-bit unsigned integer for seconds since epoch.
  • <i> is a 32-bit unsigned integer for the increment.

Regular Expression

data_regex

Strict Mode mongo Shell Mode
{ "$regex": "<sRegex>", "$options": "<sOptions>" } /<jRegex>/<jOptions>
  • <sRegex> is a string of valid JSON characters.
  • <jRegex> is a string that may contain valid JSON characters and unescaped double quote (") characters, but may not contain unescaped forward slash (/) characters.
  • <sOptions> is a string containing the regex options represented by the letters of the alphabet.
  • <jOptions> is a string that may contain only the characters ‘g’, ‘i’, ‘m’ and ‘s’ (added in v1.9). Because the JavaScript and mongo Shell representations support a limited range of options, any nonconforming options will be dropped when converting to this representation.

OID

data_oid

Strict Mode mongo Shell Mode
{ "$oid": "<id>" } ObjectId( "<id>" )

<id> is a 24-character hexadecimal string.

DB Reference

data_ref

Strict Mode mongo Shell Mode
{ "$ref": "<name>", "$id": "<id>" } DBRef("<name>", "<id>")
  • <name> is a string of valid JSON characters.
  • <id> is any valid extended JSON type.

Undefined Type

data_undefined

Strict Mode mongo Shell Mode
{ "$undefined": true } undefined

The representation for the JavaScript/BSON undefined type.

You cannot use undefined in query documents. Consider the following document inserted into the people collection:

db.people.insert( { name : "Sally", age : undefined } )

The following queries return an error:

db.people.find( { age : undefined } )
db.people.find( { age : { $gte : undefined } } )

However, you can query for undefined values using $type, as in:

db.people.find( { age : { $type : 6 } } )

This query returns all documents for which the age field has value undefined.

MinKey

data_minkey

Strict Mode mongo Shell Mode
{ "$minKey": 1 } MinKey

The representation of the MinKey BSON data type that compares lower than all other types. See Comparison/Sort Order for more information on comparison order for BSON types.

MaxKey

data_maxkey

Strict Mode mongo Shell Mode
{ "$minKey": 1 } MinKey

The representation of the MaxKey BSON data type that compares higher than all other types. See Comparison/Sort Order for more information on comparison order for BSON types.

NumberLong

New in version 2.6.

data_numberlong

Strict Mode mongo Shell Mode
{ "$numberLong": "<number>" } NumberLong( "<number>" )

NumberLong is a 64 bit signed integer. You must include quotation marks or it will be interpreted as a floating point number, resulting in a loss of accuracy.

For example, the following commands insert 9223372036854775807 as a NumberLong with and without quotation marks around the integer value:

db.json.insert( { longQuoted : NumberLong("9223372036854775807") } )
db.json.insert( { longUnQuoted : NumberLong(9223372036854775807) } )

When you retrieve the documents, the value of longUnquoted has changed, while longQuoted retains its accuracy:

> db.json.find()
{ "_id" : ObjectId("54ee1f2d33335326d70987df"), "longQuoted" : NumberLong("9223372036854775807") }
{ "_id" : ObjectId("54ee1f7433335326d70987e0"), "longUnquoted" : NumberLong("-9223372036854775808") }

最新文章

  1. C编译: 动态连接库 (.so文件)(转摘)
  2. android 一个简单的服务例子
  3. USACO2016Splitting the Field分割牧场
  4. Android AutoLayout全新的适配方式 堪称适配终结者(转)
  5. JNI技术基础(2)——从零开始编写JNI代码
  6. Java Web项目调优原则
  7. VIM Taglist安装配置和使用
  8. SQL行列转换:报表_公司采购表_每个公司各采购了些什么产品
  9. 阿里云PHP Redis代码示例
  10. 【Servlet】Filter过滤器的编写和配置
  11. Bootstrap教程
  12. jquery中的全局事件
  13. java处理Excel文件---excel文件的创建,删除,写入,读取
  14. C++零食:使用Unicode版的预定义宏__FUNCTION__
  15. 基于Windows下python环境变量配置
  16. raw_input与input
  17. idea构建spring源码阅读环境
  18. Python re 模块
  19. day03 数据类型与运算符
  20. CodeForces 286E Ladies&#39; Shop 多项式 FFT

热门文章

  1. swift 与 指针初级使用
  2. Qt之图标切分与合并
  3. ARM体系架构下的同步操作
  4. 终于可以发布Delphi下收点OSGI精髓皮毛而设计的插件框架WisdomPluginFramework
  5. C++学习笔记之输入、输出和文件
  6. C#操作Excel(1)Excel对象模型
  7. Redis for Windows(C#缓存)安装和使用
  8. 窗口界面编程之一:VB实现简单异形窗口
  9. Android下用Properties保存程序配置
  10. 读懂IL代码就这么简单