I have a DB with an entities
collection with a single document. The following mongosh
works ok:
$ mongosh mongodb://localhost:27017/ftest --eval 'db.entities.find()' --quiet
[
{
_id: { id: 'E1', type: 'E', servicePath: '/' },
attrNames: [ 'position' ],
attrs: {
position: {
value: { type: 'Point', coordinates: [ '5.5', '-6.6' ] },
type: 'geo:json',
creDate: 1702639380.716183,
modDate: 1702639380.716183,
mdNames: []
}
},
creDate: 1702639380.716183,
modDate: 1702639380.716183,
location: {
attrName: 'position',
coords: { type: 'Point', coordinates: [ 5.5, -6.6 ] }
},
lastCorrelator: '4e2f7a0e-9b3c-11ee-98d3-080027cd35f1'
}
]
However, I need a JSON document no the JavaScript representation of it (i.e the above output doesn't directly validate on typical validators).
I have tried with the --json relaxed
or --json canonical
options, but I get this error:
$ mongosh mongodb://localhost:27017/ftest --eval 'db.entities.find()' --quiet --json relaxed
{
"message": "Converting circular structure to EJSON:\n (root) -> _mongo -> __serviceProvider -> mongoClient -> s -> sessionPool -> client\n \\-------------------------------/",
"stack": "BSONError: Converting circular structure to EJSON:\n (root) -> _mongo -> __serviceProvider -> mongoClient -> s -> sessionPool -> client\n \\-------------------------------/\n at ft (/tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:983314)\n at /tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:984642\n at ft (/tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:985421)\n at /tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:984642\n at ft (/tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:985421)\n at /tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:984642\n at ft (/tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:985421)\n at /tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:984642\n at ft (/tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:985421)\n at /tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:984642\n at ft (/tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:985421)\n at /tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:984642\n at ft (/tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:985421)\n at Object.yt [as stringify] (/tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:986405)\n at t.formatForJSONOutput (/tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:10:53732)\n at CliRepl.loadCommandLineFilesAndEval (/tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:10:32114)\n at async CliRepl.start (/tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:10:29909)\n at async w (/tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:10:80067)",
"name": "BSONError"
}
$ mongosh mongodb://localhost:27017/ftest --eval 'db.entities.find()' --quiet --json canonical
{
"message": "Converting circular structure to EJSON:\n (root) -> _mongo -> __serviceProvider -> mongoClient -> s -> sessionPool -> client\n \\-------------------------------/",
"stack": "BSONError: Converting circular structure to EJSON:\n (root) -> _mongo -> __serviceProvider -> mongoClient -> s -> sessionPool -> client\n \\-------------------------------/\n at ft (/tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:983314)\n at /tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:984642\n at ft (/tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:985421)\n at /tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:984642\n at ft (/tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:985421)\n at /tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:984642\n at ft (/tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:985421)\n at /tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:984642\n at ft (/tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:985421)\n at /tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:984642\n at ft (/tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:985421)\n at /tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:984642\n at ft (/tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:985421)\n at Object.yt [as stringify] (/tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:1960:986405)\n at t.formatForJSONOutput (/tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:10:53732)\n at CliRepl.loadCommandLineFilesAndEval (/tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:10:32114)\n at async CliRepl.start (/tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:10:29909)\n at async w (/tmp/m/boxednode/mongosh/node-v20.9.0/out/Release/node:10:80067)",
"name": "BSONError"
}
As far as I understand my document (see it above) doesn't have any circular dependency... so I don't understand why I'm getting these errors.
So:
mongosh
provides output as actual JSON instead of JavaScript objects/arrays?Thanks in advance!
You can use JSON.stringify(). This should print desired output:
mongosh mongodb://localhost:27017/ftest --eval 'JSON.stringify(db.entities.find().toArray(), null, " ")' --quiet
Note, if you use findOne()
, then toArray()
is not needed.
If you want to export a collection or part of a collection into JSON, then you can also use mongoexport tool.