Skip to content

Instantly share code, notes, and snippets.

@alexbeletsky
Created May 6, 2014 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexbeletsky/20520a7ea0af852fcd4a to your computer and use it in GitHub Desktop.
Save alexbeletsky/20520a7ea0af852fcd4a to your computer and use it in GitHub Desktop.
var config = require('../config');
var db = require('./db')(config);
function readUsers(callback) {
db.users.find({unsubscribed: {$exists: false}, firstTimeUser: {$exists: false}}).forEach(function (err, user) {
if (err) {
throw callback(err);
}
if (!user) {
return callback();
}
console.log(toCsv(user));
});
function toCsv(user) {
return user.email + ',' + user.name + ',' + (user.displayName || '');
}
}
readUsers(function (err) {
if (err) {
console.error(err);
}
db.close();
});
var through = require('through');
var config = require('../config');
var db = require('./db')(config);
function readUsers(callback) {
var csv = through(function (user) {
var line = user.email + ',' + user.name + ',' + (user.displayName || '') + '\n';
this.queue(line);
});
var stream = db.users
.find({unsubscribed: {$exists: false}, firstTimeUser: {$exists: false}})
.sort({_id: -1});
stream
.pipe(csv)
.pipe(process.stdout);
stream.on('error', callback);
stream.on('end', callback);
}
readUsers(function (err) {
if (err) {
console.error(err);
}
db.close();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment