File write operations in mongo script?

Solution 1:

You could use print and then redirect output:

script.js:

cursor = db.users.find();
while(cursor.hasNext()){
    printjson(cursor.next());
}

then run the script and redirect output to a file:

mongo --quiet script.js > result.txt

Solution 2:

http://www.mongodb.org/display/DOCS/Scripting+the+shell paragraph "Differences between scripted and interactive/ Printing".

./mongo server.com/mydb --quiet --eval "db.users.find().forEach(printjson);" > 1.txt