I need to export mongodb aggregation query result to file. I am connected to remote server so I need something like db.myCollection.aggregate([...]).printToFile('C:\Downloads\resultFile.txt')
Is it possible?
Add quotes to your connection string and use printjson()
to print JSON objects. Note that the aggregate()
returns a cursor and thus you have to loop through them. If your don't use forEach
, it only returns top 20.
Here is a one line command.
mongo --quiet "mongodb://junior:SECRETPASSWORD@mongo4:9000,mongo5:9000/WebApp?authSource=admin&replicaSet=rs0&readPreference=secondaryPreferred" --eval 'db.cars.aggregate([...]).forEach(function(doc) { printjson(doc);})' > output.txt
In more readable multi-line format
mongo --quiet \
"mongodb://junior:SECRETPASSWORD@mongo4:9000,mongo5:9000/WebApp?authSource=admin&replicaSet=rs0&readPreference=secondaryPreferred" \
--eval 'db.cars.aggregate([...]).forEach(function(doc) \
{ printjson(doc);})' > output.txt
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments