@@ -54,26 +54,69 @@ The following drivers use the Extended JSON v2.0
54
54
:columns: 3
55
55
56
56
- C
57
-
58
57
- C++
59
-
60
58
- Go
61
-
62
59
- Java
63
-
64
60
- Node
65
-
66
61
- Perl
67
-
68
62
- PHPC
69
-
70
63
- Python
71
-
72
64
- Scala
73
65
74
66
For C# and Ruby that use Legacy MongoDB Extended JSON v1, refer to
75
67
:doc:`/reference/mongodb-extended-json-v1`.
76
68
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
+
77
120
MongoDB Database Tools
78
121
~~~~~~~~~~~~~~~~~~~~~~
79
122
@@ -107,8 +150,8 @@ representations in *Canonical* and *Relaxed*.
107
150
- :bsontype:`Regular Expression`
108
151
- :bsontype:`Timestamp`
109
152
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>`__ .
112
155
113
156
.. bsontype:: Array
114
157
@@ -119,7 +162,6 @@ https://github.com/mongodb/specifications/blob/master/source/extended-json.rst#c
119
162
:class: border-table
120
163
121
164
* - Canonical
122
-
123
165
- Relaxed
124
166
125
167
* - .. code-block:: none
@@ -137,7 +179,6 @@ Where the array elements are as follows:
137
179
- ``<elements>``
138
180
139
181
- Array elements use Extended JSON.
140
-
141
182
- To specify an empty array, omit the content ``[ ]``.
142
183
143
184
.. _extended-json-binary:
@@ -151,7 +192,6 @@ Where the array elements are as follows:
151
192
:class: border-table
152
193
153
194
* - Canonical
154
-
155
195
- Relaxed
156
196
157
197
* - .. code-block:: none
@@ -588,8 +628,15 @@ Where the values are as follows:
588
628
- A positive integer for the increment.
589
629
590
630
591
- Example
592
- -------
631
+ Examples
632
+ --------
633
+
634
+ The following examples illustrate Extended JSON usage.
635
+
636
+ .. _type-representations:
637
+
638
+ Type Representations
639
+ ~~~~~~~~~~~~~~~~~~~~
593
640
594
641
.. list-table::
595
642
:widths: 20 40 40
@@ -655,3 +702,106 @@ Example
655
702
* - "timestampField":
656
703
- {"$timestamp":{"t":1565545664,"i":1}}
657
704
- {"$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