Pada artikel Halo Node.js!!! saya sudah menulis dasar dari Node.js dan sedikit mengenai Express dimana keduanya merupakan unsur EN dalam MEAN stack. Tulisan ini akan membawa pembaca melangkah ke MongoDB yang merupakan unsur M.
Tutorial yang berhubungan dengan database isinya biasanya mengenai 4 huruf sakti ini : CRUD. Dan memang benar karena saya akan berbagi soal CRUD MongoDB di Node.js menggunakan Mongoose.
Apa itu Mongoose?, orang-orang sih bilangnya dia itu object modelling MongoDB untuk Node.js, di dunia sql ya ORM semacam Hibernate (java) dan Entity Framework (.NET).
Dependency
Sebelum bisa bermain dengan Mongoose tentunya kita harus mendapatkannya terlebih dahulu. Caranya tambahkan modul Mongoose pada file package.json seperti berikut
1 2 3 4 5 6 7 |
{ "name" : "BelajarMongoose", "dependencies" : { "express":"4.12.0", "mongoose" : "3.8.24" } } |
Kalau kamu masih bingung setelah tambah dependency mesti ngapain supaya modul-modul baru terinstal berarti kamu harus baca Halo Node.js terlebih dahulu.
Sekarang kita sudah punya modul Mongoose, berarti kita sudah bisa konek ke MongoDB. Pertama import dulu modulnya kemudian lakukan koneksi.
1 2 |
var mongoose=require("mongoose"); mongoose.connect("localhost:27017/mean"); |
Model
Kita membutuhkan sebuah model yang merupakan representasi dari dokumen MongoDB. Dengan model ini kita bisa melakukan CRUD. Di Mongoose, kita menggunakan Schema untuk membuat model.
1 2 3 4 5 6 7 8 9 |
var mongoose=require("mongoose"); var Schema=mongoose.Schema; var commentSchema=new Schema({ author:String, text:String }); var Comment=mongoose.model("Comment",commentSchema); |
Create
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
// SETUP var express=require("express"); var mongoose=require("mongoose"); var app=express(); mongoose.connect("localhost:27017/mean"); var Schema=mongoose.Schema; var commentSchema=new Schema({ author:String, text:String }); var Comment=mongoose.model("Comment",commentSchema); //SETUP app.get("/",function(req,res){ var newComment=new Comment({ "author" : "Negoro", "text" : "Selamat Belajar" }); newComment.save(function(error){ if(error) return res.send(error); res.send("saved"); }); }); |
Read
Read semua data
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//SETUP ..... //SETUP app.get("/",function(req, res){ Comment.find({},function(error, comments){ if(error) return res.send(error); res.json(comments); }); }); |
Read by field
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//SETUP ... //SETUP app.get("/", function(req, res){ Comment.find({"author":"Agung Setiawan"}, function(error, comment){ if(error) return res.send(error); res.json(comment); }) }); |
Read by ID
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//SETUP ... //SETUP app.get("/",function(req, res){ Comment.findById("54166742724f3580189531ce",function(error, comment){ if(error) return res.send(error); return res.json(comment); }); }); |
Update
Read by ID baru kemudian Update
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
//SETUP ... //SETUP app.get("/",function(req, res){ Comment.findById("54166742724f3580189531ce",function(error,comment){ if(error) return res.send(error); comment.text="Updated Text"; comment.save(function(error){ if(error) return res.send(error); res.json(comment); }); }); }); |
Read by Field DAN Update
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//SETUP ... //SETUP app.get("/",function(req, res){ Comment.findOneAndUpdate({"author" : "Negoro"},{"author" : "Negoro Loro","text" : "Selamat Bermain"},function(error){ if(error) return res.send(error); res.send("updated"); }); }); |
Read By ID DAN Update
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//SETUP ... //SETUP app.get("/",function(req, res){ Comment.findByIdAndUpdate("54166742724f3580189531ce",{"text" : "majuuu"}, function (error) { if(error) return res.send(error); res.send("updated"); }); }); |
Delete
Ready By ID Kemudian Hapus
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
//SETUP ... //SETUP app.get("/",function(req, res){ Comment.findById("54efdb87c8210600093dc238",function(error,comment){ if(error) return res.send(error); comment.remove(function (error) { if(error) return res.send(error); res.send("deleted"); }); }); }); |
Ready by Field DAN Hapus
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//SETUP ... //SETUP app.get("/",function(req, res){ Comment.findOneAndRemove({"author":"Anonymous"},function(error){ if(error) return res.send(error); res.send("deleted"); }) }); |
Read by ID DAN Hapus
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//SETUP ... //SETUP app.get("/",function(req, res){ Comment.findByIdAndRemove("54165e3febf5f9b8066c37a0", function (error){ if(error) return res.send(error); res.send("deleted"); }); }); |