Skip to content

Instantly share code, notes, and snippets.

@basicdays
Last active December 24, 2015 11:08
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 basicdays/6788495 to your computer and use it in GitHub Desktop.
Save basicdays/6788495 to your computer and use it in GitHub Desktop.
Sample mongodb co usage
'use strict';
var co = require('co'),
MongoClient = require('mongodb').MongoClient;
function getConnection(connectionString) {
return function(next) {
MongoClient.connect(connectionString, next);
}
}
co(function *() {
try {
var db = yield getConnection('mongodb://localhost/snmp');
var collection = yield co.wrap(db.collection, db)('rawscandata5');
var query = collection.find();
var count = yield co.wrap(query.count, query)();
console.log(count);
db.close();
} catch (ex) {
console.error(ex);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment