0

I am using MongoDB at mLab. I have multiple collections - 1 main and other supporting. Therefore, the main collection consists of IDs pointing to supporting collections. I would like to export the actual data from the main collection to a CSV file. So I need to populate the data first and then export the result.

I see I can export collections individually but then the data are not populated. I suppose I should use bash script to do this but I do not know how.

Could you point me the right direction or suggest a way to do this?

Thank you!

mosses
  • 351
  • 3
  • 18

1 Answers1

0

Using the mongo shell will be the better idea in your case, as per the official documents below is the steps to write the bash script to read the data from mongo collection in bash shell scripts:

Simple example to get the data count from a collection with updated date time with greater than 10 days.

 DATE2=$(date -d '10 days ago' "+%Y-%m-%dT%H:%M:%S.%3NZ");
 counter = $(mongo --quiet dbName --eval 'db.dbCollection.find({"updatedAt":{"$gt":new ISODate("'$DATE'")}}).count()')
 echo counter;

Or you can get the list of data and iterate over it to populate it as per your requirements.

For more on mongo shell query click here

krishna Prasad
  • 3,541
  • 1
  • 34
  • 44