Mongodb-introduction to MongoDB, MongoDB Extended JSON

Source: Internet
Author: User
Tags mongo shell mongodb compass



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.
    • MONGOShell mode. The MongoDB internal JSON parser and theMONGOShell 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 Formatinput in Strict Mode


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


    • REST Interfaces
    • Mongoimport
    • --queryoption of various MongoDB tools
    • MongoDB Compass


Other JSON parsers, includingMONGOShell anddb.eval (), can parse strict mode representations as Key/va Lue pairs, but without recognition of the type information.


Input inMONGOShell Mode


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


    • REST Interfaces
    • Mongoimport
    • --queryoption of various MongoDB tools
    • MONGOShell
Output in Strict mode


Mongoexportand REST and HTTP Interfaces output data in Strict mode.


Output inMONGOShell Mode


bsondumpoutputs inMONGOShell mode.





BSON Data Types and associated representations


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


Binary


Data_binary


Strict Mode MONGO Shell Mode
{"$binary": "<bindata>", "& #36; 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's a hexadecimal string, and in Shell mode It's an integer. See the extended Bson documentation.
Date


Data_date


Strict Mode MONGO Shell Mode
{"$date": "<date>"} New Date (<date>)


In Strict mode,<date>are an ISO-8601 date format with a mandatory time zone field following the T Emplateyyyy-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 ' stime_ttype can hold, the following format is US Ed


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


In Shell mode,<date>is the JSON representation of a 64-bit signed integer giving the number of M Illiseconds 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>", "& #36; Options": "<sOptions>"} /<jregex>/<joptions>
  • <sRegex>is a string of valid JSON characters.
  • <jRegex>is a string the may contain valid JSON characters and unescaped double quote (") charact ERS, but could 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 theJavaScriptandMONGO Shell Representations support a limited range of options, any nonconforming options would be dropped if 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>", "& #36; 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 useundefinedin query documents. Consider the following document inserted into thepeoplecollection:


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 the$type, as in:


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


This is a query returns all documents forwhich the age field has valueundefined.


Minkey


Data_minkey


Strict Mode MONGO Shell Mode
{"$minKey": 1} Minkey


The representation of the Minkey BSON data type is compares lower than all other types. See Comparison/sort Order For more information in Comparison order for BSON types.


Maxkey


Data_maxkey


Strict Mode MONGO Shell Mode
{"$minKey": 1} Minkey





The representation of the Maxkey BSON data type is compares higher than all other types. See Comparison/sort Order For more information in Comparison order for BSON types.


Numberlong


New in version 2.6.



Data_numberlong


Strict Mode MONGO Shell Mode
{"$numberLong": "<number>"} Numberlong ("<number>")


Numberlongis a A-bit signed integer. You must include quotation marks or it'll be interpreted as a floating point number, resulting in a loss of accuracy.



For example, the following commands inserts9223372036854775807as anumberlongwith and without Quotatio n marks around the integer value:


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


When you retrieve the documents, the value oflongunquotedhave changed, whilelongquotedretains its ACC Uracy:


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



Mongodb-introduction to MongoDB, MongoDB Extended JSON


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.