Tuesday, January 24, 2023

CRUD MongoDB Pada Node.js

 


1. Insert 

Format : (Insert Single record)

1. Insert Single Record

var MongoClient = require('mongodb').MongoClient;  

var url = "mongodb://localhost:27017/ MongoDb";  

MongoClient.connect(url, function(err, db) {  

if (err) throw err;  

var myobj = { name: "Kumar", age: "28", address: "India" };  

db.collection("employees").insertOne(myobj, function(err, res) {  

if (err) throw err;  

console.log("1 record inserted");  

db.close();  

});  

});


2. Insert Multi Record

var MongoClient = require('mongodb').MongoClient;  

var url = "mongodb://localhost:27017/ MongoDb";  

MongoClient.connect(url, function(err, db) {  

if (err) throw err;  

var myobj = [     

{ name: "Mahesh Sharma", age: "23", address: "Ghaziabad"},  

{ name: "Tom Moody", age: "43", address: "CA"},  

{ name: "Zahira Abdul", age: "18", address: "Islamabad"},  

{ name: "John Ross", age: "33", address: "London"}  

];  

db.collection("customers").insert(myobj, function(err, res) {  

if (err) throw err;  

console.log("Number of records inserted: " + res.insertedCount);  

db.close();  

});  

});


2. Select

2.1. Select Single Record

var http = require('http');  

var MongoClient = require('mongodb').MongoClient;  

var url = "mongodb://localhost:27017/MongoDb";  

MongoClient.connect(url, function(err, db) {  

  if (err) throw err;  

  db.collection("employees").findOne({}, function(err, result) {  

    if (err) throw err;  

    console.log(result.name);  

    db.close();  

  });  

});


2.2. Select Multi Record

var MongoClient = require('mongodb').MongoClient;  

var url = "mongodb://localhost:27017/MongoDb";  

MongoClient.connect(url, function(err, db) {  

  if (err) throw err;  

  db.collection("employees").find({}).toArray(function(err, result) {  

    if (err) throw err;  

    console.log(result);  

    db.close();  

  });  

});


2.3. Select Condition dengan sama dengan ( = )

var http = require('http');  

var MongoClient = require('mongodb').MongoClient;  

var url = "mongodb://localhost:27017/MongoDb";  

MongoClient.connect(url, function(err, db) {  

if (err) throw err;  

var query = { address: "Delhi" };  

db.collection("employees").find(query).toArray(function(err, result) {  

if (err) throw err;  

console.log(result);  

db.close();  

});  

});


2.4. Select Condition dengan Start / Like

var http = require('http');  

var MongoClient = require('mongodb').MongoClient;  

var url = "mongodb://localhost:27017/MongoDb";  

MongoClient.connect(url, function(err, db) {  

if (err) throw err;  

var query = { address: /^L/ };  

db.collection("employees").find(query).toArray(function(err, result) {  

if (err) throw err;  

console.log(result);  

db.close();  

});  

});


2.5. Sorting /Filter

2.5.1. Sort Ascending

var http = require('http');  

var MongoClient = require('mongodb').MongoClient;  

var url = "mongodb://localhost:27017/ MongoDb";  

MongoClient.connect(url, function(err, db) {  

if (err) throw err;  

var mysort = { name: 1 };  

db.collection("employees").find().sort(mysort).toArray(function(err, result) {  

if (err) throw err;  

console.log(result);  

db.close();  

});  

});


2.5.2. Sort Descending

var http = require('http');  

var MongoClient = require('mongodb').MongoClient;  

var url = "mongodb://localhost:27017/ MongoDb";  

MongoClient.connect(url, function(err, db) {  

if (err) throw err;  

var mysort = { name: -1 };  

db.collection("employees").find().sort(mysort).toArray(function(err, result) {  

if (err) throw err;  

console.log(result);  

db.close();  

});  

});


3. Remove

var http = require('http');  

var MongoClient = require('mongodb').MongoClient;  

var url = "mongodb://localhost:27017/ MongoDb";  

MongoClient.connect(url, function(err, db) {  

if (err) throw err;  

var myquery = { address: 'London' };  

db.collection("employees").remove(myquery, function(err, obj) {  

if (err) throw err;  

console.log(obj.result.n + " record(s) deleted");  

db.close();  

});  

});


Memunculkan Simbol & Emoji Pada OS Mac

  Memunculkan Simbol & Emoji  1. Buka aplikasi Pages / Notes pada Macbook. 2. Klik pada Menubar Edit --> Pilih Emoji and Symbols a...