|
| 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