Last active
December 24, 2015 11:08
-
-
Save basicdays/6788495 to your computer and use it in GitHub Desktop.
Sample mongodb co usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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