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.