MongoDB - Introduction to MongoDB, MongoDB Extended JSON
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.
- REST Interfaces
- mongoimport
- --query option of various MongoDB tools
- MongoDB Compass
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.
- REST Interfaces
- mongoimport
- --query option of various MongoDB tools
- mongo shell
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") }
MongoDB - Introduction to MongoDB, MongoDB Extended JSON的更多相关文章
- MongoDB - Introduction to MongoDB, BSON Types
BSON is a binary serialization format used to store documents and make remote procedure calls in Mon ...
- MongoDB之一介绍(MongoDB与MySQL的区别、BSON与JSON的区别)
MySQL与MongoDB的操作对比,以及区别 MySQL与MongoDB都是开源的常用数据库,但是MySQL是传统的关系型数据库,MongoDB则是非关系型数据库,也叫文档型数据库,是一种NoSQL ...
- MongoDB - Introduction to MongoDB, Documents
MongoDB stores data records as BSON documents. BSON is a binary representation of JSON documents, th ...
- MongoDB学习笔记(一) MongoDB介绍及安装(摘)
MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种.它在许多场景下可用于替代传统的关系型数据库或键/值存储方式.Mongo使用C++开发.Mongo的官方网 ...
- Mongodb学习笔记一(Mongodb环境配置)
Mongodb学习 说明: MongoDB由databases组成,database由collections组成,collection由documents组成,document由fileds组成.Mo ...
- 使用MMS(MongoDB Monitoring Service)监控MongoDB
使用MMS(MongoDB Monitoring Service)监控MongoDB 一.MongoDB简介: MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可 ...
- 一点MongoDB的基础及mongodb在mac上的安装
最近发现维持写博客的习惯还是挺困难的,尤其对我来说,计划好的事过了好长时间才想到要去做. 这段时间一直在熟悉MongoDB,首先我是参考的这一篇:8天学通MongoDB 原博主写得非常好,我这里就 ...
- MongoDb进阶实践之三 Mongodb基本命令详解
一.引言 从今天开始,我要正式开始介绍MongoDB的使用方法了.在此之前,我用了两篇文章分别介绍了如何在Linux系统和Windows系统上安装和配置MongoDB系统.如 ...
- MongoDB(1)--MongoDB安装及简介
一.MongoDB的应用场景及实现原理二.MongoDB的常用命令及配置三.手写基于MongoDB的ORM框架四.基于MongoDB实现网络云盘实战五.MongoDB 4.0新特性 一.MongoDB ...
随机推荐
- JQuery 弹出层,始终显示在屏幕正中间
1.让层始终显示在屏幕正中间: 样式代码: .model{ position: absolute; z-index: 1003; width:320px; height:320px; text-ali ...
- Django官方文档学习1——第一个helloworld页面
Django 1.10官方文档:https://docs.djangoproject.com/en/1.10/intro/tutorial01/ 1.查看django版本 python -m djan ...
- Linux下getopt()函数的简单使用
最近在弄Linux C编程,本科的时候没好好学啊,希望学弟学妹们引以为鉴. 好了,虽然啰嗦了点,但确实是忠告.步入正题: 我们的主角----getopt()函数. 英雄不问出处,getopt()函数的 ...
- jQuery + jQuery Mobile 实现省市二级下拉列表页面
一.需求: 提供省.市下拉列表,当用户选择省一级下拉列表项后,市下拉列表的各个选项自动变为该省对应的城市列表. 二.效果: 三.实现: 1.省市json数据,来自: http://www.cnblog ...
- Java Web目录
1. Spring持久化 2. Spring核心之IoC——依赖注入 3. Hibernate查询语言 4. Hibernate 实体关联关系映射(转载) 5. 用MyEclipse自动生成hiber ...
- Java再学习——栈(stack)和堆(heap)
一.内存分配的策略 按照编译原理的观点,程序运行时的内存分配有三种策略,分别是静态的,栈式的,和堆式的. 静态存储分配是指在编译时就能确定每个数据目标在运行时刻的存储空间需求,因而在编译时就可以给他们 ...
- selenium 一个简单的流程
在整个自动化测试过程中需要分为及部分: 1.初始化 2.结束 3.异常处理 4.截图 5.对弹窗的处理 6.测试用例 整个过程中需要包括 ...
- 开发工具 之 PowerDesigner
PowerDesigner是一款功能非常强大的建模工具软件,足以与Rose比肩,同样是当今最著名的建模软件之一.Rose是专攻UML对象模型的建模工具,之后才向数据库建模发展,而PowerDesign ...
- Python-Day11 RabbitMQ/redis
写在前面: 好久不写了,实在是不想写,坚持果然是一件不容易的事情. 我喜欢玩,我更爱学习,然而喜欢是放肆,爱是克制,哈哈.每天上班有些忙就不想动,只想在床上翻滚或者鏖战召唤师峡谷.上班闲着时想了想,一 ...
- 企业级搜索引擎Solr 第三章 索引数据(Indexing Data)[1]
转载:http://quweiprotoss.wap.blog.163.com/ Push data to Solr or have Solr pull it 尽管一个应用通过HTTP方式与Solr通 ...