Skip to content

Commit 31750db

Browse files
Merge pull request #163 from davemungo/DOCSP-19177-mongosh-output-in-non-json-form
DOCSP-19177 mongosh output not parsable
2 parents 748d021 + b67ac66 commit 31750db

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
.. code-block:: javascript
2+
3+
mongosh --quiet --host rs0/centos1104 --port 27500 \
4+
--eval "EJSON.stringify(rs.status().members.map( \
5+
m => ({'id':m._id, 'name':m.name, 'stateStr':m.stateStr})));" \
6+
| jq
7+
8+
After parsing with ``jq``, the output resembles this:
9+
10+
.. code-block:: javascript
11+
:copyable: false
12+
13+
[
14+
{
15+
"id": 0,
16+
"name": "centos1104:27500",
17+
"stateStr": "PRIMARY"
18+
},
19+
{
20+
"id": 1,
21+
"name": "centos1104:27502",
22+
"stateStr": "SECONDARY"
23+
},
24+
{
25+
"id": 2,
26+
"name": "centos1104:27503",
27+
"stateStr": "SECONDARY"
28+
}
29+
]
30+
31+
.. note::
32+
33+
``EJSON`` has built in formatting options which may eliminate the
34+
need for a parser like ``jq``. For example, the following code
35+
produces output that is formatted the same as above.
36+
37+
.. code-block:: javascript
38+
:emphasize-lines: 3
39+
40+
mongosh --quiet --host rs0/centos1104 --port 27500 \
41+
--eval "EJSON.stringify( rs.status().members.map( \
42+
({ _id, name, stateStr }) => ({ _id, name, stateStr })), null, 2);"
43+

source/reference/compatibility.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,11 @@ in ``mongosh`` better align with the types used by the MongoDB Drivers.
154154
For more information on managing types, refer to the
155155
:manual:`schema validation overview </core/schema-validation>`.
156156

157+
``--eval`` Behavior
158+
-------------------
159+
160+
``mongosh --eval`` does not quote object keys in its ouptut. To get
161+
output suitable for automated parsing, use ``EJSON.stringify()``.
162+
163+
.. include:: /includes/examples/ex-eval-output.rst
164+

source/reference/options.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ General Options
3636
The result of evaluating a JavaScript argument is printed to your
3737
command line.
3838

39+
To get output suitable for automated parsing, use
40+
``EJSON.stringify()``.
41+
42+
.. include:: /includes/examples/ex-eval-output.rst
43+
3944
.. option:: --help, -h
4045

4146
Returns information on the options and use of the |mdb-shell|.

0 commit comments

Comments
 (0)