Created
August 20, 2011 01:51
-
-
Save heru/1158486 to your computer and use it in GitHub Desktop.
this gist contain how to connect mongodb from node.js using mongoose ORM
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
/** | |
* how to connect mongodb in node.js using mongoose ORM | |
* MongoDB Resources : http://mongodb.org | |
* Mongoose Resources : http://mongoosejs.com | |
**/ | |
// initialize mongoose | |
var mongoose = require('mongoose'); | |
// connection mongoose to mongodb | |
// this just like odbc or jdbc | |
// in this case mongodb server at localhost | |
// you can change easy by replace localhost with your mongodb server | |
mongoose.connect('mongodb://localhost/hellomongo'); | |
// get mongodb schema | |
var Schema = mongoose.Schema, ObjectId = Schema.ObjectId; | |
//create new Collection Schema | |
// if you new to mongodb, think Collection as database table | |
var UserCollection = new Schema({ | |
username : String, | |
password : String, | |
last_access : Date | |
}); | |
// mapping schema with model | |
var UserModel = mongoose.model('User', UserCollection); | |
// insert a new user | |
var heru = new UserModel(); | |
heru.username = 'heru'; | |
heru.password = 'heru password'; | |
heru.last_access = new Date(); | |
// save instance | |
heru.save(function(err){ | |
if(!err){ | |
console.log('user saved'); | |
}else{ | |
console.log('error while save user, err : ' + err); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment