Export result of Mongodb query to file

Čamo

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?

simagix

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to export a query result to a txt-file

Export cassandra query result to a csv file

Export query result in Pervasive to txt / csv file

Export query result to Json

Export query result to .csv file in SQL Server 2008

How to export hive query result to single local file?

How to export query result to text file using command line

Export SQL query result to CSV

How can I store the result of MongoDB query in a text file with Nodejs?

Limiting Query result in MongoDB

Aggregate query result in mongodb

Waiting for MongoDB query result

Mongodb Export Query using Wildcards?

Oracle SQL Developer query result Export Wizard default output file name

Export Query Result as CSV file from Docker PostgreSQL container to local machine

Invoked Toad and executed query file from command line. Cannot automatically export result dataset

Export query result to csv in oracle stored procedure

Export Result of Native Query to Excel JPA

NDepend: how to export the result from a query

mongoose query result can not export with data object

Passing string result to query then export as csv

No result from MongoDB / MongoJS query

find with wildcard path and export the result to a file

How to adjust a file (to remove []) to export result in NetLogo?

Export result R data frame into an Excel file

Read a file in bash, execute query in MongoDB and add result in the last column in a while loop

cannot insert query result into file

MongoDB - Export - export all the documents matching the specific field - that are listed in a file

Show MongoDB Query Result On ejs page