Reputation: 10823
I want to output MongoDB records to static JSON so I can load it in another program for analysis.
I got as far as:
mongo "mongodb://root:$MONGO_PASSWORD@$MONGO_CONNECT" users.js
with users.js
looking like:
cursor = db.users.find()
while (cursor.hasNext()) {
printjson(cursor.next())
}
But the stdout has a couple of problems. It includes the Mongo shell output and connection info. And the JSON isn't valid / properly concatenated.
What's a better approach to getting JSON out of Mongo?
Upvotes: 2
Views: 1518
Reputation: 49945
I think mongoexport is what you're looking for. It outputs your collection as a text format:
Try
mongoexport --db yourdb --collection users --uri "mongodb://root:$MONGO_PASSWORD@$MONGO_CONNECT" --out users.json
Will output:
{_id: "id1", field1: "field1Val...", ...}
{_id: "id2", field1: "field1Val...", ...}
So each line will contain single document from your collection
Upvotes: 1