Skip to content

Commit 0052daa

Browse files
jason-price-mongodbjason-price-mongodb
andauthored
DOCSP-32655-EJSON-deserialize (#304)
* DOCSP-32655-EJSON-deserialize * DOCSP-32655-EJSON-deserialize * DOCSP-32655-EJSON-deserialize * DOCSP-32655-EJSON-deserialize * DOCSP-32655-EJSON-deserialize * DOCSP-32655-EJSON-deserialize * DOCSP-32655-EJSON-deserialize * DOCSP-32655-EJSON-deserialize * DOCSP-32655-EJSON-deserialize * DOCSP-32655-EJSON-deserialize * DOCSP-32655-EJSON-deserialize * DOCSP-32655-EJSON-deserialize * DOCSP-32655-EJSON-deserialize * DOCSP-32655-EJSON-deserialize * DOCSP-32655-EJSON-deserialize * DOCSP-32655-EJSON-deserialize * DOCSP-32655-EJSON-deserialize * DOCSP-32655-EJSON-deserialize * DOCSP-32655-EJSON-deserialize * DOCSP-32655-EJSON-deserialize * DOCSP-32655-EJSON-deserialize * DOCSP-32655-EJSON-deserialize * DOCSP-32655-EJSON-deserialize * DOCSP-32655-EJSON-deserialize --------- Co-authored-by: jason-price-mongodb <[email protected]>
1 parent 7d60322 commit 0052daa

File tree

3 files changed

+173
-1
lines changed

3 files changed

+173
-1
lines changed

source/crud.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Perform CRUD Operations
66

77
.. default-domain:: mongodb
88

9-
.. facet::
9+
.. facet::
1010
:name: genre
1111
:values: reference
1212

source/reference/ejson.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@ to transform BSON data.
2424

2525
.. list-table::
2626
:header-rows: 1
27+
:widths: 30, 70
2728

2829
* - EJSON Method
2930
- Use
3031

32+
* - :ref:`EJSON.deserialize() <mongosh-ejson-deserialize>`
33+
- Convert Extended JSON objects to BSON objects. This method
34+
is useful to import JSON data from external applications.
35+
3136
* - :ref:`EJSON.stringify() <mongosh-ejson-stringify>`
3237
- Convert BSON objects to strings. This method is useful to
3338
transform ``mongosh`` output.
@@ -52,6 +57,7 @@ Learn More
5257
.. toctree::
5358
:titlesonly:
5459

60+
/reference/ejson/deserialize
5561
/reference/ejson/parse
5662
/reference/ejson/serialize
5763
/reference/ejson/stringify
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
.. _mongosh-ejson-deserialize:
2+
3+
===================
4+
EJSON.deserialize()
5+
===================
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. facet::
12+
:name: programming_language
13+
:values: shell
14+
15+
.. contents:: On this page
16+
:local:
17+
:backlinks: none
18+
:depth: 2
19+
:class: singlecol
20+
21+
.. include:: /includes/links-urls/ejson.rst
22+
23+
The ``EJSON.deserialize()`` method converts Extended JSON objects to
24+
BSON objects.
25+
26+
Syntax
27+
------
28+
29+
The method has this syntax:
30+
31+
.. code-block:: javascript
32+
:copyable: false
33+
34+
EJSON.deserialize( object, [ options ] )
35+
36+
Method Fields
37+
-------------
38+
39+
The method takes the following fields:
40+
41+
.. list-table::
42+
:header-rows: 1
43+
44+
* - Field
45+
- Type
46+
- Necessity
47+
- Description
48+
49+
* - ``object``
50+
- EJSON object
51+
- Required
52+
- EJSON object to convert. For example, an array of documents.
53+
54+
* - ``options``
55+
- string
56+
- Optional
57+
- Modifies the output object :ref:`types <mongo-shell-data-type>`.
58+
The only option is ``{ relaxed: <boolean> }``.
59+
60+
.. list-table::
61+
:header-rows: 1
62+
:widths: 30 70
63+
64+
* - Boolean Value
65+
- Description
66+
67+
* - ``true``
68+
- Return plain JavaScript object types. Default is
69+
``true``.
70+
71+
* - ``false``
72+
- Return BSON object types.
73+
74+
Behavior
75+
--------
76+
77+
You can run ``EJSON.deserialize()`` from an interactive ``mongosh``
78+
session or from the system command line using ``--eval``.
79+
80+
To run ``EJSON.deserialize()`` from an interactive ``mongosh`` session,
81+
use:
82+
83+
.. code-block:: javascript
84+
:copyable: false
85+
86+
EJSON.deserialize( object, [ options ] )
87+
88+
To run ``EJSON.deserialize()`` from the system command line, use:
89+
90+
.. code-block:: shell
91+
:copyable: false
92+
93+
mongosh --eval "EJSON.deserialize( object, [ options ] )"
94+
95+
Example
96+
-------
97+
98+
Create the ``sales`` collection:
99+
100+
.. code-block:: javascript
101+
102+
db.sales.insertMany( [
103+
{ custId: 345, purchaseDate: ISODate("2023-07-04"),
104+
quantity: 4, cost: Decimal128("100.60") },
105+
{ custId: 346, purchaseDate: ISODate("2023-07-12"),
106+
quantity: 3, cost: Decimal128("175.45") },
107+
{ custId: 486, purchaseDate: ISODate("2023-08-01"),
108+
quantity: 9, cost: Decimal128("200.53") }
109+
] )
110+
111+
The following sections show how to create an example file and then
112+
import the file with an ``EJSON.deserialize()`` example.
113+
114+
Create the Example File
115+
~~~~~~~~~~~~~~~~~~~~~~~
116+
117+
The following example retrieves the ``sales`` documents as an array and
118+
saves the results to a file named ``salesDeserialize.json`` on the
119+
computer's file system:
120+
121+
.. code-block:: javascript
122+
123+
let salesCollection = EJSON.stringify( db.sales.find().toArray() )
124+
125+
fs.writeFileSync( 'salesDeserialize.json', salesCollection )
126+
127+
Import the Example File from the Command Line
128+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
129+
130+
To import the ``salesDeserialize.json`` file and create a new collection
131+
named ``salesFromDeserializeFile``, exit ``mongosh`` and then run this
132+
example from the command line:
133+
134+
.. code-block:: sh
135+
136+
# Note: The example is formatted to fit the page.
137+
138+
mongosh --quiet --eval "db.salesFromDeserializeFile.insertMany( \
139+
EJSON.deserialize( JSON.parse ( \
140+
fs.readFileSync( 'salesDeserialize.json', 'utf8' ) ) ) )"
141+
142+
In the example:
143+
144+
- ``fs.readFileSync()`` reads the ``salesDeserialize.json`` file and
145+
interprets the content as ``utf8`` Unicode character strings.
146+
147+
- ``JSON.parse()`` converts the strings read from the file to JSON.
148+
149+
- ``EJSON.deserialize()`` outputs JSON.
150+
151+
- ``db.salesFromDeserializeFile.insertMany()`` creates and populates the
152+
``salesFromDeserializeFile`` collection using the JSON returned by
153+
``EJSON.deserialize()``.
154+
155+
.. note::
156+
157+
You could use ``EJSON.parse()`` in the previous example, which is a
158+
simpler approach. ``JSON.parse()`` and ``EJSON.deserialize()``
159+
provide more flexibility.
160+
161+
Learn More
162+
----------
163+
164+
- `EJSON deserialize method
165+
<https://github.com/mongodb/js-bson#EJSON.deserialize>`__
166+
- |ejsonUrl| documentation

0 commit comments

Comments
 (0)