Print out field from MongoDB and save in file

Today I encountered a fun little problem. I had to run through a database and print out every single field of the query and save it in a text document.

So I ended up doing it like this.

First I wrote a script that looks like this:

db.collection.find({active: true}).forEach(
    function(x) {

Then I ran the script like this:

`mongo databaseName script.js | sed -n -e ‘1,1000p’ > testing.txt`

I added the sed-command to print out the first 1000 lines. This approach worked out well, and I can add sort and uniq if I need that.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s