Created
September 5, 2011 11:29
-
-
Save fengmk2/1194742 to your computer and use it in GitHub Desktop.
mongoskin CRUD demo
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
var mongo = require('mongoskin'); | |
var db = mongo.db('127.0.0.1:27017/test'); | |
// create index: | |
// key, unique, callback | |
db.collection('user').ensureIndex([['name', 1]], true, function(err, replies){}); | |
// bind method: db.user ===> db.collection('user') | |
db.bind('user'); | |
// insert, update, remove | |
var username = 'user_' + new Date().getTime(); | |
var user = {name: username, age: 18, tweet: 'hello world, I am ' + username, created_at: new Date()}; | |
db.user.insert(user, function(err) { | |
if(err) { | |
return console.log('inser error', err); | |
} | |
console.log('inserted, user: ', user); | |
db.user.update({_id: user._id}, {$set: {tweet: 'haha update', updated_at: new Date()}}, function(err) { | |
if(err) { | |
return console.log('update error', err); | |
} | |
db.user.findOne({_id: user._id}, function(err, updated_user) { | |
if(err) { | |
return console.log('findOne error:', err); | |
} | |
console.log('updated user:', updated_user); | |
db.user.remove({_id: user._id}, function(err) { | |
if(err) { | |
return console.log('remove error:', err); | |
} | |
db.user.findOne({_id: user._id}, function(err, remove_user) { | |
if(err) { | |
return console.log('findOne error:', err); | |
} | |
console.log('remove user:', remove_user); | |
}); | |
}); | |
}); | |
}); | |
}); | |
function newUser(i, callback) { | |
var username = 'user_' + i; | |
var user = {name: username, age: 18, tweet: 'hello world, I am ' + username, created_at: new Date()}; | |
db.user.insert(user, function(err) { | |
callback(); | |
}); | |
}; | |
function createUsers(count, callback) { | |
var done = 0; | |
var fn = function() { | |
if(++done >= count) { | |
callback(); | |
} else { | |
newUser(done, fn); | |
} | |
}; | |
newUser(done, fn); | |
} | |
createUsers(45, function() { | |
// query: find all user, sort by name desc, limit 20, 10 | |
db.user.find().sort({name: -1}).skip(20).limit(10).toArray(function(err, users) { | |
console.log(err, users); | |
}); | |
}); |
changing your connection string by adding mongodb:// fixes the problem...
var db = mongo.db('mongodb://127.0.0.1:27017/test');
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This crashes on line 7 for me: here is the callstack
Debugging
mongoSkinTest.js
debugger listening on port 5858
/Users/paul/projects/ddApp/node_modules/mongodb/lib/mongodb/connection/url_parser.js:15
throw Error("URL must be in the format mongodb://user:pass@host:port/dbnam
^
Error: URL must be in the format mongodb://user:pass@host:port/dbname
at Error ()
at exports.parse (/Users/paul/projects/ddApp/node_modules/mongodb/lib/mongodb/connection/url_parser.js:15:11)
at Function.MongoClient.connect (/Users/paul/projects/ddApp/node_modules/mongodb/lib/mongodb/mongo_client.js:164:16)
at SkinClass.SkinDb._open (/Users/paul/projects/ddApp/node_modules/mongoskin/lib/db.js:36:25)
at SkinClass.exports.makeSkinClass.SkinClass.open (/Users/paul/projects/ddApp/node_modules/mongoskin/lib/utils.js:162:14)
at SkinClass.SkinCollection._open (/Users/paul/projects/ddApp/node_modules/mongoskin/lib/collection.js:49:17)
at SkinClass.exports.makeSkinClass.SkinClass.open (/Users/paul/projects/ddApp/node_modules/mongoskin/lib/utils.js:162:14)
at SkinClass.exports.makeSkinClass.SkinClass._bindMethod.SkinClass.(anonymous function) as ensureIndex
at Object. (/Users/paul/projects/ddApp/mongoSkinTest.js:7:23) <-- your code
at Module._compile (module.js:456:26)