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.

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") }
时间: 2024-10-11 23:45:59

MongoDB - Introduction to MongoDB, MongoDB Extended JSON的相关文章

MongoDB学习笔记(一) MongoDB介绍及安装

这是MongoDB的系列学习笔记的第一篇,主要介绍什么是非关系型数据库MongoDB,如何下载,去哪儿下载,又该怎么正确的安装等一系列问题. 一.前言 最近开始学习非关系型数据库MongoDB,却在博客园上找不到比较系统的教程,很多资料都要去查阅英文网站,效率比较低下.本人不才,借着自学的 机会把心得体会都记录下来,方便感兴趣的童鞋分享讨论.部分资源出自其他博客,旨将零散知识点集中到一起,如果有侵犯您的权利,请联系li- [email protected].大部分内容均系原创,欢迎大家转载分享,

【MongoDB】01、MongoDB基础

一.MongoDB概述 1.MogoDB简介 什么是MongoDB ? MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB 旨在为WEB应用提供可扩展的高性能数据存储解决方案. MongoDB 将数据存储为一个文档,数据结构由键值(key=>value)对组成.MongoDB 文档类似于 JSON 对象.字段值可以包含其他文档,数组及文档数组. 一个文档相当于mysql数据库中的一行数据 200

Mongodb学习笔记一(Mongodb环境配置)

mongodb学习 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* BLOCKS =============================================================================*/ p, blockquote, ul, ol, dl, table, pre { margin: 15px 0;

MongoDB学习笔记(一) MongoDB介绍及安装(摘)

MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种.它在许多场景下可用于替代传统的关系型数据库或键/值存储方式.Mongo使用C++开发.Mongo的官方网站地址是:http://www.mongodb.org/,读者可以在此获得更详细的信息. 小插曲:什么是NoSql? NoSql,全称是 Not Only Sql,指的是非关系型的数据库.下一代数据库主要解决几个要点:非关系型的.分布式的.开源的.水平可扩展的.原始的目的是为了大规模web应用,这场

57-1、2、3 NoSQL基础及MongoDB、MongoDB基本应用、mongodb索引及复制集

02MongoDB基本应用 配置环境: node1:192.168.1.121CentOS release 6.7 [[email protected] ~]# cd mongodb-2.6.4/ [[email protected] mongodb-2.6.4]# ls mongodb-org-2.6.4-1.x86_64.rpm         mongodb-org-shell-2.6.4-1.x86_64.rpm mongodb-org-mongos-2.6.4-1.x86_64.rpm

Mongodb学习笔记二(Mongodb基本命令)

一.Mongodb命令 说明:Mongodb命令是区分大小写的,使用的命名规则是驼峰命名法. 对于database和collection无需主动创建,在插入数据时,如果database和collection不存在则会自动创建. 常用命令 help命令通过此命令可以看到一些最基本的命令,如图: use命令例如命令[use demodb],创建demodb,不用担心demodb不会创建,当使用use demodb 命令创建第一个collection时会自动创建数据库demodb,如图: 插入数据使用

深入浅出MongoDB(三)mongoDB下载和安装

软件下载 下载mongodb最新的包:http://www.mongodb.org/downloads 下载mongodb可视化界面,mongoVUE:http://download.csdn.net/detail/lovesummerforever/8036417 软件安装 mongodb服务端安装步骤如下所示 1.遵守mongodb规范,选择自己的磁盘创建文件夹,在这里我选择的是D盘,创建如下目录结构 D:\mongodb  作为安装的根目录 D:\mongodb\data 数据目录 D:\

MongoDB系列:把mongodb作为windows的服务来启动

1.首先切换到mongodb安装目录下的bin目录,在控制台直接运行以下命令 "C:\Program Files\MongoDB\Server\3.0\bin\mongod.exe" --logpath "D:\mongodb\logs\mongodb.log" --dbpath "D:\mongodb\data" --install --journal ps: 1.必须用管理员身份在命令后运行控制台 2.d盘下必须先创建好mongodb/dat

windows平台使用MongoDB shell 来连接 MongoDB 服务器并创建数据库

windows平台使用MongoDB shell 来连接 MongoDB 服务器并创建数据库 命令行进入MongoDB的bin目录运行mongod.exe mongod --dbpath c:\data\db 或者找到MongoDB的bin目录双击运行mongod.exe.待输出 I NETWORK [thread1] waiting for connections on port 27017. 命令行进入MongoDB的bin目录运行: mongo 打开一个连接(默认为本地连接) 这时候你返回