Koneksi MongoDB di Node.js menggunakan Mongoose ORM


Berikut ini adalah contoh koneksi mongodb di Node.js dengan menggunakan mongoose ORM

Informasi lebih detail tentang nodejs bisa di lihat di http://nodejs.org,, mongodb bisa di chek di http://mongodb.org sedangkan mongoose ORM bisa di lihat di sini

/**
* 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);
  }
});

source code bisa di lihat https://gist.github.com/1158486

One thought on “Koneksi MongoDB di Node.js menggunakan Mongoose ORM

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s