Skip to content

Commit 18adfff

Browse files
jason-price-mongodbjason-price-mongodb
andauthored
DOCSP-32654-serialize-method (#302)
* DOCSP-32654-serialize-method * DOCSP-32654-serialize-method * DOCSP-32654-serialize-method * DOCSP-32654-serialize-method * DOCSP-32654-serialize-method * DOCSP-32654-serialize-method * DOCSP-32654-serialize-method * DOCSP-32654-serialize-method * DOCSP-32654-serialize-method * DOCSP-32654-serialize-method * DOCSP-32654-serialize-method * DOCSP-32654-serialize-method * DOCSP-32654-serialize-method * DOCSP-32654-serialize-method * DOCSP-32654-serialize-method * DOCSP-32654-serialize-method * DOCSP-32654-serialize-method * DOCSP-32654-serialize-method --------- Co-authored-by: jason-price-mongodb <[email protected]>
1 parent 5ce3c78 commit 18adfff

File tree

3 files changed

+178
-2
lines changed

3 files changed

+178
-2
lines changed

source/changelog.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,7 @@ This release adds support for the following APIs:
11851185
:issue:`MONGOSH-307 <MONGOSH-307>`.
11861186

11871187

1188-
- Free monitoring commands such as :method:`db.enableFreeMonitoring()`.
1188+
- Free monitoring commands such as ``db.enableFreeMonitoring()``.
11891189
More detail in :issue:`MONGOSH-300 <MONGOSH-300>`.
11901190

11911191
- Logging and profiling helper method implementations

source/reference/ejson.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ to transform BSON data.
3232
- Convert BSON objects to strings. This method is useful to
3333
transform ``mongosh`` output.
3434

35+
* - :ref:`EJSON.serialize() <mongosh-ejson-serialize>`
36+
- Convert BSON objects to Extended JSON representation as
37+
JavaScript objects. This method is useful to export JSON data for
38+
external data transformation applications.
39+
3540
* - :ref:`EJSON.parse() <mongosh-ejson-parse>`
3641
- Convert strings to JSON. This method is useful to transform
3742
inputs.
@@ -48,5 +53,5 @@ Learn More
4853
:titlesonly:
4954

5055
/reference/ejson/parse
56+
/reference/ejson/serialize
5157
/reference/ejson/stringify
52-

source/reference/ejson/serialize.txt

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

0 commit comments

Comments
 (0)