Skip to content

Instantly share code, notes, and snippets.

@hajovsky
Created April 18, 2014 13:42
Show Gist options
  • Save hajovsky/11044854 to your computer and use it in GitHub Desktop.
Save hajovsky/11044854 to your computer and use it in GitHub Desktop.
yieldable mongodb native driver
var mongo = require('mongodb');
var format = require('util').format;
var co = require('co');
const MONGO_URL = 'mongodb://127.0.0.1:27017/test';
// MongoClient.connect(MONGO_URL, function(err, db) {
// if(err) throw err;
// var collection = db.collection('test_insert');
// collection.insert({a:2}, function(err, docs) {
// collection.count(function(err, count) {
// console.log(format("count = %s", count));
// });
// collection.find().toArray(function(err, results) {
// console.dir(results);
// db.close();
// });
// });
// });
var y = require('yieldable');
y([
mongo.MongoClient,
mongo.Collection
]);
co(function*() {
var client = new mongo.MongoClient();
var db = yield client.y$connect(MONGO_URL);
console.log("connected");
var collection = db.collection('test_insert');
var docs = yield collection.y$insert({a:2});
console.log("docs", docs);
var count = yield collection.y$count();
console.log(format("count = %s", count));
// cursor returned by find() must be wrapped, wrapping mongodb.Cursor directly doesn't work
var results = yield y(collection.find()).y$toArray();
console.dir(results);
db.close();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment