Skip to content

Commit ff5a1ce

Browse files
author
Dave
authored
Docsp 15626 ejson output for explain v5.3 (#528)
* DOCSP-15626 ejson output * Tweeks * example layout changes * Review feedback * Layout * Layout * Review feedback * Review feedback * Review feedback
1 parent 0e71f9d commit ff5a1ce

File tree

1 file changed

+165
-15
lines changed

1 file changed

+165
-15
lines changed

source/reference/mongodb-extended-json.txt

Lines changed: 165 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,69 @@ The following drivers use the Extended JSON v2.0
5454
:columns: 3
5555

5656
- C
57-
5857
- C++
59-
6058
- Go
61-
6259
- Java
63-
6460
- Node
65-
6661
- Perl
67-
6862
- PHPC
69-
7063
- Python
71-
7264
- Scala
7365

7466
For C# and Ruby that use Legacy MongoDB Extended JSON v1, refer to
7567
:doc:`/reference/mongodb-extended-json-v1`.
7668

69+
Extended JSON Methods
70+
~~~~~~~~~~~~~~~~~~~~~
71+
72+
MongoDB provides the following methods for Extended JSON:
73+
74+
.. list-table::
75+
76+
* - Method
77+
- Description
78+
79+
* - ``serialize``
80+
- Serializes a BSON object and returns the data in Extended JSON
81+
format.
82+
83+
.. code-block:: javascript
84+
85+
EJSON.serialize( db.<collection>.findOne() )
86+
87+
* - ``deserialize``
88+
- Converts a serialized document to field and value pairs. The
89+
values have :ref:`BSON types <type-representations>`.
90+
91+
.. code-block:: javascript
92+
93+
EJSON.deserialize( <serialized object> )
94+
95+
* - ``stringify``
96+
- Converts the element and :ref:`type <type-representations>`
97+
pairs in a deserialized object to strings.
98+
99+
.. code-block:: javascript
100+
101+
EJSON.stringify( <deserialized object> )
102+
103+
* - ``parse``
104+
- Converts strings into element and :ref:`type
105+
<type-representations>` pairs.
106+
107+
.. code-block:: javascript
108+
109+
EJSON.parse( <string> )
110+
111+
For usage examples, see :ref:`ex-obj-conversions` below.
112+
113+
For additional details, see the documentation for:
114+
115+
- `MongoDB NodeJS Driver
116+
<https://mongodb.github.io/node-mongodb-native/4.0/>`__
117+
- `BSON Parser <https://github.com/mongodb-js/bson-ext>`__
118+
- `BSON-EXT Parser <https://github.com/mongodb-js/bson-ext>`__
119+
77120
MongoDB Database Tools
78121
~~~~~~~~~~~~~~~~~~~~~~
79122

@@ -107,8 +150,8 @@ representations in *Canonical* and *Relaxed*.
107150
- :bsontype:`Regular Expression`
108151
- :bsontype:`Timestamp`
109152

110-
For a complete list, see
111-
https://github.com/mongodb/specifications/blob/master/source/extended-json.rst#conversion-table.
153+
The complete list is `here
154+
<https://github.com/mongodb/specifications/blob/master/source/extended-json.rst#conversion-table>`__.
112155

113156
.. bsontype:: Array
114157

@@ -119,7 +162,6 @@ https://github.com/mongodb/specifications/blob/master/source/extended-json.rst#c
119162
:class: border-table
120163

121164
* - Canonical
122-
123165
- Relaxed
124166

125167
* - .. code-block:: none
@@ -137,7 +179,6 @@ Where the array elements are as follows:
137179
- ``<elements>``
138180

139181
- Array elements use Extended JSON.
140-
141182
- To specify an empty array, omit the content ``[ ]``.
142183

143184
.. _extended-json-binary:
@@ -151,7 +192,6 @@ Where the array elements are as follows:
151192
:class: border-table
152193

153194
* - Canonical
154-
155195
- Relaxed
156196

157197
* - .. code-block:: none
@@ -588,8 +628,15 @@ Where the values are as follows:
588628
- A positive integer for the increment.
589629

590630

591-
Example
592-
-------
631+
Examples
632+
--------
633+
634+
The following examples illustrate Extended JSON usage.
635+
636+
.. _type-representations:
637+
638+
Type Representations
639+
~~~~~~~~~~~~~~~~~~~~
593640

594641
.. list-table::
595642
:widths: 20 40 40
@@ -655,3 +702,106 @@ Example
655702
* - "timestampField":
656703
- {"$timestamp":{"t":1565545664,"i":1}}
657704
- {"$timestamp":{"t":1565545664,"i":1}}
705+
706+
.. _ex-obj-conversions:
707+
708+
Extended JSON Object Conversions
709+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
710+
711+
The following short examples create a document object and then convert
712+
the object to different forms using Extended JSON object conversion
713+
methods.
714+
715+
Setup
716+
`````
717+
718+
Create a document in the ``conversions`` collection:
719+
720+
.. code-block:: javascript
721+
722+
db.conversions.insertOne( { insertDate: new Date() } )
723+
724+
:binary:`mongosh` returns a document object:
725+
726+
.. code-block:: javascript
727+
728+
{
729+
acknowledged: true,
730+
insertedId: ObjectId("61fbaf25671c45f3f5f4074a")
731+
}
732+
733+
EJSON.serialize
734+
```````````````
735+
736+
Serialize the data stored in a MongoDB document object:
737+
738+
.. code-block:: javascript
739+
740+
serialized = EJSON.serialize( db.conversions.findOne() )
741+
742+
:binary:`mongosh` parses a JavaScript object and returns values using
743+
``"$"`` prefixed :ref:`types <type-representations>`:
744+
745+
.. code-block:: javascript
746+
747+
{
748+
_id: { '$oid': '61fbaf25671c45f3f5f4074a' },
749+
insertDate: { '$date': '2022-02-03T10:32:05.230Z' }
750+
}
751+
752+
EJSON.deserialize
753+
`````````````````
754+
755+
Deserialize a serialized object:
756+
757+
.. code-block:: javascript
758+
759+
EJSON.deserialize( serialized )
760+
761+
:binary:`mongosh` parses a JavaScript object and returns values using
762+
the default :binary:`mongosh` :ref:`type <type-representations>` form:
763+
764+
.. code-block:: javascript
765+
766+
{
767+
_id: new ObjectId( "61fbaf25671c45f3f5f4074a" ),
768+
insertDate: ISODate( "2022-02-03T10:32:05.230Z" )
769+
}
770+
771+
EJSON.stringify
772+
```````````````
773+
774+
Convert an object to a string:
775+
776+
.. code-block:: javascript
777+
778+
stringified = EJSON.stringify( db.conversions.findOne() )
779+
780+
:binary:`mongosh` outputs the elements of the converted object as
781+
strings:
782+
783+
.. code-block:: javascript
784+
785+
{
786+
"_id": {"$oid":"61fbaf25671c45f3f5f4074a"},
787+
"insertDate":{"$date":"2022-02-03T10:32:05.230Z"}
788+
}
789+
790+
EJSON.parse
791+
```````````
792+
793+
Parse a string to create an object:
794+
795+
.. code-block:: javascript
796+
797+
EJSON.parse( stringified )
798+
799+
:binary:`mongosh` returns the converted strings as documents:
800+
801+
.. code-block:: javascript
802+
803+
{
804+
_id: new ObjectId("61fbaf25671c45f3f5f4074a"),
805+
insertDate: ISODate("2022-02-03T10:32:05.230Z")
806+
}
807+

0 commit comments

Comments
 (0)