MongoDB Shell
1 result found
-
mongosh and JSON: normalized output
The CLI commands, like mongosh, can be used in bash scripts to quickly implement reliable data processing pipelines. Thanks to the aggregation pipeline your clients can do pretty complex things with this, despite a small number of lines and zero infrastructure. Unfortunately, in the recent versions, the output of those commands is no longer JSON. When mongosh returns results, it is now in pseudo-JSON, which can no longer be parsed directly by other standard tools like jq. This makes interpreting the results needlessly complicated. In addition, error messages are not output in JSON either (simple unformatted strings in that case),…
2 votesStarting with mongosh 1.6.0, it's now possible to pass a `--json` parameter to mongosh in conjunction with
`--eval`. For example:
$ mongosh $CONNECTION_STRING --eval 'db.coll.stats()' --json=relaxed --quiet | jq '{storageSize,totalSize}'
{
"storageSize": 36864,
"totalSize": 184320
}If instead you are looking for a way to output JSON from a script, you can use `EJSON.stringify(thingToOutput, null, 2)`.
- Don't see your idea?