A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.mongodb.com/docs/manual/reference/mongodb-extended-json/ below:

MongoDB Extended JSON (v2) - Database Manual

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

Both formats conform to the JSON RFC and can be parsed by the various MongoDB drivers and tools.

The following drivers support Extended JSON v2.0:

MongoDB provides the following methods for Extended JSON:

Method

Description

serialize

Serializes a BSON object and returns the data in Extended JSON format.

EJSON.serialize( db.<collection>.findOne() )

deserialize

Converts a serialized document to field and value pairs. The values have BSON types.

EJSON.deserialize( <serialized object> )

stringify

Converts the element and type pairs in a deserialized object to strings.

EJSON.stringify( <deserialized object> )

parse

Converts strings into element and type pairs.

EJSON.parse( <string> )

For usage examples, see Extended JSON Object Conversions below.

For additional details, see the documentation for:

Starting in version 4.2:

Binary

Changes

bsondump

Uses Extended JSON v2.0 (Canonical mode) format.

mongodump

Use Extended JSON v2.0 (Canonical mode) format for the metadata. Requires mongorestore version 4.2 or later that supports Extended JSON v2.0 (Canonical mode or Relaxed) format.

In general, use corresponding versions of mongodump and mongorestore. To restore data files created with a specific version of mongodump, use the corresponding version of mongorestore.

mongoexport

Creates output data in Extended JSON v2.0 (Relaxed mode) by default.

Creates output data in Extended JSON v2.0 (Canonical mode) if used with --jsonFormat.

mongoimport

Expects import data to be in Extended JSON v2.0 (either Relaxed or Canonical mode) by default.

Can recognize data that is in Extended JSON v1.0 format if the option --legacy is specified.

In general, the versions of mongoexport and mongoimport should match. To import data created from mongoexport, you should use the corresponding version of mongoimport.

The following presents some common BSON data types and the associated representations in Canonical and Relaxed.

The complete list is here.

Array

Canonical

Relaxed

[ <elements> ]
<Same as Canonical>

Where the array elements are as follows:

Binary

Canonical

Relaxed

{ "$binary":   {      "base64": "<payload>",      "subType": "<t>"   }}
<Same as Canonical>

Where the values are as follows:

Date

For dates between years 1970 and 9999, inclusive:

Canonical

Relaxed

{"$date": {"$numberLong": "<millis>"}}
{"$date": "<ISO-8601 Date/Time Format>"}

For dates before year 1970 or after year 9999:

Canonical

Relaxed

{"$date": {"$numberLong": "<millis>"}}
<Same as Canonical>

Where the values are as follows:

Decimal128

Canonical

Relaxed

{ "$numberDecimal": "<number>" }
<Same as Canonical>

Where the values are as follows:

Document

Canonical

Relaxed

{ <content> }
<Same as Canonical>

Where the document contents are as follows:

Double

For finite numbers:

Canonical

Relaxed

{"$numberDouble": "<decimal string>" }
<non-integer number>

For infinite numbers or NAN:

Canonical

Relaxed

{"$numberDouble": <"Infinity"|"-Infinity"|"NaN"> }
<Same as Canonical>

Where the values are as follows:

Int64

Canonical

Relaxed

{ "$numberLong": "<number>" }
<integer>

Where the values are as follows:

Int32

Canonical

Relaxed

{ "$numberInt": "<number>" }
<integer>

Where the values are as follows:

MaxKey

Canonical

Relaxed

{ "$maxKey": 1 }
<Same as Canonical>

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

MinKey

Canonical

Relaxed

{ "$minKey": 1 }
<Same as Canonical>

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

ObjectId

Canonical

Relaxed

{ "$oid": "<ObjectId bytes>" }
<Same as Canonical>

Where the values are as follows:

Regular Expression

Canonical

Relaxed

{ "$regularExpression":   {      "pattern": "<regexPattern>",      "options": "<options>"  }}
<Same as Canonical>

Where the values are as follows:

Timestamp

Canonical

Relaxed

{"$timestamp": {"t": <t>, "i": <i>}}
<Same as Canonical>

Where the values are as follows:

The following examples illustrate Extended JSON usage.

Example Field Name

Canonical Format

Relaxed Format

"_id":

{"$oid":"5d505646cf6d4fe581014ab2"}

{"$oid":"5d505646cf6d4fe581014ab2"}

"arrayField":

["hello",{"$numberInt":"10"}]

["hello",10]

"dateField":

{"$date":{"$numberLong":"1565546054692"}}

{"$date":"2019-08-11T17:54:14.692Z"}

"dateBefore1970":

{"$date":{"$numberLong":"-1577923200000"}}

{"$date":{"$numberLong":"-1577923200000"}}

"decimal128Field":

{"$numberDecimal":"10.99"}

{"$numberDecimal":"10.99"}

"documentField":

{"a":"hello"}

{"a":"hello"}

"doubleField":

{"$numberDouble":"10.5"}

10.5

"infiniteNumber"

{"$numberDouble":"Infinity"}

{"$numberDouble":"Infinity"}

"int32field":

{"$numberInt":"10"}

10

"int64Field":

{"$numberLong":"50"}

50

"minKeyField":

{"$minKey":1}

{"$minKey":1}

"maxKeyField":

{"$maxKey":1}

{"$maxKey":1}

"regexField":

{"$regularExpression":{"pattern":"^H","options":"i"}}

{"$regularExpression":{"pattern":"^H","options":"i"}}

"timestampField":

{"$timestamp":{"t":1565545664,"i":1}}

{"$timestamp":{"t":1565545664,"i":1}}

"uuid":

{"$uuid":"3b241101-e2bb-4255-8caf-4136c566a962"}

{"$uuid":"3b241101-e2bb-4255-8caf-4136c566a962"}

The following short examples create a document object and then convert the object to different forms using Extended JSON object conversion methods.

Create a document in the conversions collection:

db.conversions.insertOne( { insertDate: new Date() } )

mongosh returns a document object:

{  acknowledged: true,  insertedId: ObjectId("61fbaf25671c45f3f5f4074a")}

Serialize the data stored in a MongoDB document object:

serialized = EJSON.serialize( db.conversions.findOne() )

mongosh parses a JavaScript object and returns values using "$" prefixed types:

{  _id: { '$oid': '61fbaf25671c45f3f5f4074a' },  insertDate: { '$date': '2022-02-03T10:32:05.230Z' }}

Deserialize a serialized object:

EJSON.deserialize( serialized )

mongosh parses a JavaScript object and returns values using the default mongosh type form:

{  _id: new ObjectId( "61fbaf25671c45f3f5f4074a" ),  insertDate: ISODate( "2022-02-03T10:32:05.230Z" )}

Convert an object to a string:

stringified = EJSON.stringify( db.conversions.findOne() )

mongosh outputs the elements of the converted object as strings:

{   "_id": {"$oid":"61fbaf25671c45f3f5f4074a"},   "insertDate":{"$date":"2022-02-03T10:32:05.230Z"}}

Parse a string to create an object:

EJSON.parse( stringified )

mongosh returns the converted strings as documents:

{  _id: new ObjectId("61fbaf25671c45f3f5f4074a"),  insertDate: ISODate("2022-02-03T10:32:05.230Z")}

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4