-
Notifications
You must be signed in to change notification settings - Fork 51
DOCSP-48390 Document Data Formats: EJSON #1069
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
691b493
38c0fce
bd76c83
cb976a4
c126f62
4b9dfa9
9675fa9
a1666e8
72f0019
8c4971e
86af804
8833125
704e27c
da4b6f2
2ba171c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
.. _node-extended-json: | ||
|
||
============================ | ||
Work with Extended JSON Data | ||
============================ | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: code examples, bson, relaxed, canonical, legacy | ||
|
||
Overview | ||
-------- | ||
|
||
JSON is a data format that represents the values of objects, arrays, numbers, | ||
strings, booleans, and nulls. The **Extended JSON** format defines a reserved | ||
set of keys prefixed with "``$``" to represent field type information that | ||
directly corresponds to each type in BSON, the format that MongoDB uses to | ||
store data. | ||
|
||
Extended JSON Formats | ||
--------------------- | ||
|
||
MongoDB Extended JSON features different string formats to represent BSON data. | ||
Each of the different formats conform to the JSON RFC | ||
and meet specific use cases. The **Extended** format, also known as the | ||
**Canonical** format, features specific representations for every BSON type | ||
for bidirectional conversion without loss of information. The **Relaxed Mode** | ||
format is more concise and closer to ordinary JSON, but does not represent | ||
all the type information such as the specific byte size of number fields. | ||
lindseymoore marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
See the following table to see a description of each format: | ||
|
||
.. list-table:: | ||
:header-rows: 1 | ||
:stub-columns: 1 | ||
:widths: 10 40 | ||
|
||
* - Name | ||
- Description | ||
|
||
* - **Extended** | ||
- | Also known as the *Canonical* format, this JSON representation avoids loss of | ||
BSON type information. | ||
| This format prioritizes type preservation at the loss of human-readability and | ||
interoperability with older formats. | ||
|
||
* - **Relaxed Mode** | ||
- | JSON representation that describes BSON documents with some type information loss. | ||
| This format prioritizes human-readability and interoperability at the loss of | ||
certain type information. | ||
|
||
To learn more about JSON, BSON, and Extended JSON, see | ||
`our article about JSON and BSON <https://www.mongodb.com/resources/basics/json-and-bson>`__ | ||
and :manual:`Extended JSON </reference/mongodb-extended-json/>` in the {+mdb-server+} manual. | ||
|
||
.. _extended_json_example_section: | ||
|
||
Extended JSON Examples | ||
~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
The following examples show a document containing an ObjectId, date, and long | ||
number field represented in the Extended JSON format. Click the tab that | ||
corresponds to the format of the example you want to see: | ||
|
||
.. tabs:: | ||
|
||
.. tab:: Extended | ||
:tabid: extended-format | ||
|
||
.. code-block:: json | ||
|
||
{ | ||
"_id": { "$oid": "573a1391f29313caabcd9637" }, | ||
"createdAt": { "$date": { "$numberLong": "1601499609" }}, | ||
"numViews": { "$numberLong": "36520312" } | ||
} | ||
|
||
.. tab:: Relaxed Mode | ||
:tabid: relaxed-mode-format | ||
|
||
.. code-block:: json | ||
|
||
{ | ||
"_id": { "$oid": "573a1391f29313caabcd9637" }, | ||
"createdAt": { "$date": "2020-09-30T18:22:51.648Z" }, | ||
"numViews": 36520312 | ||
} | ||
|
||
Write Extended JSON | ||
------------------- | ||
|
||
You can write an Extended JSON string from a BSON document object by using the | ||
``EJSON.stringify()`` method. | ||
|
||
The following example outputs an Extended JSON string in the Relaxed format: | ||
|
||
.. io-code-block:: | ||
|
||
.. input:: | ||
:language: js | ||
|
||
const { Binary, Code, EJSON } = require('bson'); | ||
lindseymoore marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
doc = [ | ||
{"foo": [1, 2]}, | ||
{"bar": {"hello": "world"}}, | ||
{"code": new Code("function x() { return 1; }", {})}, | ||
{"bin": Binary.createFromBase64("AQIDBA==", 0)} | ||
] | ||
lindseymoore marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const ejson_str = EJSON.stringify(doc); | ||
console.log(ejson_str); | ||
lindseymoore marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. output:: | ||
:language: none | ||
:visible: false | ||
|
||
[{"foo":[1,2]},{"bar":{"hello":"world"}},{"code":{"$scope":{},"$code":"function x() { return 1; }"}},{"bin":{"$binary":{"base64":"AQIDBA==","subType":"00"}}}] | ||
|
||
By default, the ``stringify()`` method returns the Extended JSON string in the | ||
Relaxed format. To specify the Canonical format, set the ``relaxed`` | ||
option to ``false``. | ||
|
||
The following example shows how to output Extended JSON in the Canonical format: | ||
|
||
.. io-code-block:: | ||
|
||
.. input:: | ||
:language: js | ||
|
||
const { Binary, Code, EJSON } = require('bson'); | ||
lindseymoore marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
doc = [ | ||
{"foo": [1, 2]}, | ||
{"bar": {"hello": "world"}}, | ||
{"code": new Code("function x() { return 1; }", {})}, | ||
{"bin": Binary.createFromBase64("AQIDBA==", 0)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same mods as above. Maybe we should replace the binary example with a Date one? we have the "$numberInt"s showing up here, but the difference between Date relaxed true/false is more significant, it demonstrates how one is readable and the other is intended more for transmission/storage. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replaced with a Date object. Let me know what you think! |
||
] | ||
|
||
const ejson_str = EJSON.stringify(doc, { relaxed: false }); | ||
print(ejson_str) | ||
|
||
.. output:: | ||
:language: none | ||
:visible: false | ||
|
||
[{"foo":[{"$numberInt":"1"},{"$numberInt":"2"}]},{"bar":{"hello":"world"}},{"code":{"$scope":{},"$code":"function x() { return 1; }"}},{"bin":{"$binary":{"base64":"AQIDBA==","subType":"00"}}}] | ||
nbbeeken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Read Extended JSON | ||
------------------ | ||
|
||
You can read an Extended JSON string into the JavaScript value or object described | ||
by the string by using the ``EJSON.parse()`` method. | ||
|
||
The following example shows how you can read an Extended JSON string into a | ||
JavaScript value or object by using the ``parse()`` method: | ||
|
||
.. io-code-block:: | ||
|
||
.. input:: | ||
:language: javascript | ||
|
||
import { BSON } from 'mongodb'; | ||
const EJSON = BSON.EJSON; | ||
nbbeeken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const ejson_str = '[{"foo":[1,2]},{"bar":{"hello":"world"}},{"code":{"$code":"function x() { return 1; }","$scope":{}}},{"bin":{"$binary":{"base64":"AQIDBA==","subType":"00"}}}]' | ||
|
||
const doc = EJSON.parse(ejson_str); | ||
lindseymoore marked this conversation as resolved.
Show resolved
Hide resolved
|
||
console.log(doc) | ||
|
||
.. output:: | ||
:language: none | ||
:visible: false | ||
|
||
[ | ||
{ foo: [ 1, 2 ] }, | ||
{ bar: { hello: 'world' } }, | ||
{ code: new Code('function x() { return 1; }', {}) }, | ||
{ bin: Binary.createFromBase64('AQIDBA==', 0) } | ||
] | ||
lindseymoore marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. note:: | ||
|
||
The driver parses the ``$uuid`` Extended JSON type from a string to a | ||
``BsonBinary`` object of binary subtype 4. For more information about ``$uuid`` field | ||
parsing, see the | ||
:spec:`Special Rules for Parsing $uuid Fields </extended-json/extended-json.md#special-rules-for-parsing-uuid-fields>` | ||
section in the extended JSON specification. | ||
|
||
API Documentation | ||
----------------- | ||
|
||
To learn more about any of the methods or types discussed in this | ||
guide, see the `EJSON <https://github.com/mongodb/js-bson#ejson>`__ API documentation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[q] Do you know if the different format names are meant to be capitalized? I think it makes sense to do so, but I've seen a mix in different docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to be capitalized in the Server Docs, so I tried to be consistent: https://www.mongodb.com/docs/manual/reference/mongodb-extended-json/