[MongoDB] How to output query results to standard output and save to file

Tadashi Shigeoka ·  Wed, April 9, 2014

Here’s a command to output MongoDB query results to standard output and save them to a file.

MongoDB | モンゴディービー

mongo --quiet dbname ./export_users.js > users.tsv

The —quiet option prevents unnecessary information like version display from being output.

By the way, export_users.js is a mongo script like this:

var users = db.users.find();

users.forEach(function(user) {
  if (user) {
    print(user._id + '\  ' + user.name + '\  ' + user.email);
  }
});

That’s it.

That’s all from the Gemba.