Skip to content

Commit 0bd6bf0

Browse files
authored
mongosh and mongo quoting differences (#308)
* change section header and example to be more generic * reword * external review changes
1 parent a13b1fa commit 0bd6bf0

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

source/reference/compatibility.txt

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,38 @@ in ``mongosh`` better align with the types used by the MongoDB Drivers.
202202
For more information on managing types, refer to the
203203
:manual:`schema validation overview </core/schema-validation>`.
204204

205-
``--eval`` Behavior
206-
-------------------
205+
Object Quoting Behavior
206+
-----------------------
207207

208-
``mongosh --eval`` does not quote object keys in its output.
208+
``mongosh`` does not quote object keys in its output, and places single
209+
quotes on values. To reproduce the output of the legacy
210+
``mongo`` shell, which wraps both the keys and string values in double quotes,
211+
use ``mongosh --eval`` with :ref:`EJSON.stringify() <mongosh-ejson-stringify>`.
209212

210-
.. include:: /includes/examples/ex-eval-output.rst
213+
For example, the following command returns the items in the ``sales``
214+
collection on the ``test`` database with double quotes and indentation:
215+
216+
.. code-block:: shell
217+
218+
mongosh --eval "EJSON.stringify(db.sales.findOne().toArray(), null, 2)"
219+
220+
The output looks like the following:
221+
222+
.. code-block:: javascript
223+
224+
{
225+
"_id": {
226+
"$oid": "64da90c1175f5091debcab26"
227+
},
228+
"custId": 345,
229+
"purchaseDate": {
230+
"$date": "2023-07-04T00:00:00Z"
231+
},
232+
"quantity": 4,
233+
"cost": {
234+
"$numberDecimal": "100.60"
235+
}
236+
}
211237

212238
Limitations on Database Calls
213239
-----------------------------

source/reference/ejson/stringify.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ To control how documents are passed to EJSON, use one of the ``mongosh``
155155
Examples
156156
--------
157157

158-
To try these examples, first create the ``sales`` collection:
158+
To try these examples, first create a ``sales`` collection in the ``test``
159+
database:
159160

160161
.. code-block:: javascript
161162

0 commit comments

Comments
 (0)