Powerful, most of drivers include mongoskin build upon it, but node-mongodb-native has awkward syntax, too many callback, and we need a way to hold Collection instance as Model for MVC.
It provide an ORM way to hold Collection instance as Model, you should define schema first. But why mongodb need schema? Some guys like me, want to write code from application layer but not database layer, and we can use any fields without define it before.
Mongoose provide a DAL that you can do validation, and write your middlewares. But some guys like me would like to validate manually, I think it is the tao of mongodb.
If you don't thinks so, Mongoose-ORM is probably your choice.
Mongoskin is an easy to use driver of mongodb for nodejs, it is similar with mongo shell, powerful like node-mongodb-native, and support additional javascript method binding, which make it can act as a Model(in document way).
It will provide full features of node-mongodb-native, and make it future.
npm install mongoskin
Is mongoskin synchronized?
Nop! It is asynchronized, it use future. Mongoskin is the future layer above node-mongodb-native
You can connect to mongodb easier now.
var mongo = require('mongoskin');
mongo.db('localhost:27017/testdb').collection('blog').find().toArray(function(err, items){
console.dir(items);
})
You can also set auto_reconnect
options querystring.
And native_parser options will automatically set from wheather native_parser avariable.
var mongo = require('mongoskin'),
db = mongo.db('localhost:27017/test?auto_reconnect');
You can do everything that node-mongodb-native can do.
db.createCollection(...);
db.collection('user').ensureIndex([['username', 1]], true, function(err, replies){});
db.collection('posts').hint = 'slug';
db.collection('posts').findOne({slug: 'whats-up'}, function(err, post){
// do something
});
db.collection('posts').find().toArray(function(err, posts){
// do something
});
You can bind additional methods for collection.
It is very useful if you want to use MVC patterns with nodejs and mongodb.
You can also invoke collection by properties after bind,
it could simplfy your require
.
db.bind('posts', {
findTop10 : function(fn){
this.find({}, {limit:10, sort:[['views', -1]]}).toArray(fn);
},
removeTagWith : function(tag, fn){
this.remove({tags:tag},fn);
}
});
db.bind('comments');
db.collection('posts').removeTagWith('delete', function(err, replies){
//do something
});
db.posts.findTop10(function(err, topPosts){
//do something
});
db.comments.find().toArray(function(err, comments){
//do something
});
for more information, see the source.
[*://][username:password@]host[:port][/database][?auto_reconnect[=true|false]]`
e.g.
localhost/blog
mongo://admin:pass@127.0.0.1:27017/blog?auto_reconnect
127.0.0.1?auto_reconnect=false
Bind SkinCollection to db properties. see SkinDb.bind for more information.
Get or create instance of SkinDb.
Create SkinServer of native ServerCluster. e.g.
var mongo = require('mongoskin');
var cluster = mongo.cluster('192.168.0.1:27017', '192.168.0.2:27017', '192.168.0.3:27017')
var db = cluster.db('dbname', 'admin', 'pass');
Create SkinServer of native ServerPair
Construct SkinServer from native Server instance.
Construct SkinDb from SkinServer.
Construct SkinDb.
Connect to database, retrieval native Db instance, callback is function(err, db).
Retrieval SkinCollection instance of specified collection name.
Bind SkinCollection to db properties as a shortcut to db.collection(name). You can also bind additional methods to the SkinCollection, it is useful when you want to reuse a complex operation. This will also affect db.collection(name) method.
e.g.
db.bind('book', {
firstBook: function(fn){
this.findOne(fn);
}
});
db.book.firstBook(function(err, book){});
See Db of node-mongodb-native for more information.
Retrieval native Collection instance, callback is function(err, collection).
Equivalent to
db.bson_serilizer.ObjectID.createFromHexString(hex);
See ObjectID.createFromHexString
Equivalent to
collection.find(..., function(err, cursor){
cursor.toArray(callback);
});
See Collection.find
Equivalent to
collection.find(..., function(err, cursor){
cursor.each(callback);
});
See Collection.find
Equivalent to
collection.findOne({_id, ObjectID.createFromHexString(id)}, ..., callback);
Equivalent to
collection.update({_id, ObjectID.createFromHexString(id)}, ..., callback);
If the last parameter is function, it is equivalent to native Collection.find method, else it will return a future SkinCursor.
e.g.
// callback
db.book.find({}, function(err, cursor){/* do something */});
// future SkinCursor
db.book.find().toArray(function(err, books){/* do something */});
See Collection of node-mongodb-native for more information.
checkCollectionName
count
createIndex
distinct
drop
dropIndex
dropIndexes
ensureIndex
find
findAndModify
findOne
group
indexInformation
insert
insertAll
mapReduce
normalizeHintField
options
remove
rename
save
update
See Cursor of node-mongodb-native for more information.
All these methods will return the SkinCursor itself.
sort(keyOrList, [direction], [callback])
limit(limit, [callback])
skip(skip, [callback])
batchSize(skip, [callback])
toArray(callback)
each(callback)
count(callback)
nextObject(callback)
getMore(callback)
explain(callback)