-2

I am running queries from mongo shell and get responses back on the screen. I want to get the response and store it to a file(text or js for example).

For example: to store db.persons.find({}); to a persons.js file on current path on the terminal Users/david/my_files

How can I do that?

Moyshe Zuchmir
  • 1,522
  • 12
  • 18
  • What's wrong with cut and paste? – Liam Aug 20 '21 at 13:52
  • [see this](https://docs.mongodb.com/database-tools/) , you can aggregate and store results in a collection, export that collection to bson, and convert it to a JSON file, maybe there is a faster way also – Takis Aug 20 '21 at 14:22
  • @Liam the response length is ~10,000 lines. I won't copy-paste it manually. Is there a better way? – Moyshe Zuchmir Aug 20 '21 at 15:20

1 Answers1

0

Can use mongoexport. See here.

In a nutshell:
mongoexport --host=myhost.example.com --port=37017 --username=homer --authenticationDatabase=admin --db=my_db --collection=persons --query='{}' --out=data/persons.json

Moyshe Zuchmir
  • 1,522
  • 12
  • 18