thomseddon / co-mongo Goto Github PK
View Code? Open in Web Editor NEWA mongodb wrapper that plays nicely with co.
License: MIT License
A mongodb wrapper that plays nicely with co.
License: MIT License
Hi @thomseddon, when you say:
Not all methods should be yielded, the rule is: if the methods accepts a callback (e.g. collection.insert()), yield it, if it doesn't (e.g. collection.find()) don't.
Please correct me if I'm wrong but according to this docs, fetching a mongodb collection using defaults (specifically {strict: false}
), the use of yield
should not be necessary. For example doing something like this with thunkify should work:
var MongoClient = require("mongodb").MongoClient
, thunkify = require("thunkify")
, co = require('co')
co(function *() {
MongoClient.connect = thunkify(MongoClient.connect)
var db = yield MongoClient.connect('mongodb://localhost:27017/test');
// fetch the collection without using yield
var collection = db.collection("users");
var cursor = collection.find()
cursor.toArray = thunkify(cursor.toArray);
var users = yield cursor.toArray()
console.log(users)
db.close();
});
but if you try it using co-mongo:
var comongo = require('co-mongo');
var co = require('co');
co(function *() {
var db = yield comongo.connect('mongodb://127.0.0.1:27017/test');
// try to fetch the collection without using yield
var collection = db.collection('users');
var cursor = collection.find();
var users = yield cursor.toArray();
console.log(users);
db.close();
})();
it throws an error like this:
TypeError: Object function (done) {
args.push(function (err, collection) {
if (err) return done(err);
if (Array.isArray(collection)) {
collection = collection.map(function (collection) {
return new Collection(collection);
});
return done(null, collection);
}
done(null, new Collection(collection));
});
db[method].apply(db, args);
} has no method 'find'
It might be a minor change but it could help to keep consistency with node-mongodb-native.
Cheers! :D
Hey,
I am really glad I found this project and I think this has the potential to be the next mongodb package for node :)
This just as a small note beside. What I want to report is that I think we could make a better approach for setting up the connection and to replace the current 'connect()', 'get()', 'configure()' approach:
var comongo = require('co-mongo');
// configure as usual
comongo.configure('mongodb://localhost/test')
// this is possible SYNC !!
var users = comongo.collection('users')
// use in generator
app.use(function *(next) {
this.body = yield users.findOne();
});
The reason why I am interested on this feature is that I would like to "get" the db or the collection in "non-generator" function to pass around.
For example as you see in the above snippet I use a module variable users
to cache the collection. This is unfortunately not possible as the module is not executed in a generator function.
What do you think any suggestions?
ok, this issue should be fixed easily and don't need to update "node-mongodb-native".
bulk is supported in api 1.4.x, but doesn't work with co-mongo.
we need support something like this:
var col = db.collection('my_collection');
var bulk = col.initializeUnorderedBulkOp();
//then, we can insert a lot of docs at once:
bulk.insert({_id:1, name: 1});
bulk.insert({_id:2, name: 2});
//....
yield bulk.execute();
many thanks
official api for 1.4.x:
http://mongodb.github.io/node-mongodb-native/1.4/api-generated/collection.html?highlight=bulk#initializeUnorderedBulkOp
mongodb dep should be bumped > 2.0 :)
Great library. Took a while to find - I assumed it was on GitHub (what isn't) but co-mongo
returns a lot of items. Had to use Google with "site:github.com"...
There are a lot of bugs with mongodb 1.3, wil you kind enough to update it to at lease 2.0.20? thanks.
Hey,
Cool module. Would you mind updating your npm dependency to "mongodb": "~1.4.29" so that it can support MongoDb v 3.*. You can see the docs here where this is a requirement. http://docs.mongodb.org/ecosystem/drivers/node-js/ Using this without upgrading that dependency results in a MongoError: driver is incompatible with this server version.
Thanks!
Tim
I am executing below code around 1Million times and Memory consumed by "insert" gradually get increased and program aborted when all memory consumed.
var item = yield productsCollection.insert( p );
can you fix this issue ?
Below is the screenshot of heapdump and its all filled with db objects. If i comment out insert code, loop runs fine till 10 million
Cause collection.remove is deprecated, can we please map "deleteOne" and "deleteMany"?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.