CRUD NoSQL dengan MongoDB

Dalam tulisan kali ini kita akan mencoba CRUD (Create, Read, Update dan Delete) menggunakan perintah NoSQL dengan Database Server MongoDB. Disini kita butuh komputer yang sudah terinstall Server MongoDB. Jika belum terinstall MongoDB, silakan install terlebih dahulu.

Disini kita akan membuat database perusahaan yang berisi pegawai.

Di komputer saya telah terinstall Server MongoDB di direktori C:\MongoDB\

Jalankan Server MongoDB dengan perintah :

C:\MongoDB\bin>mongo 127.0.0.1

Hasilnya seperti berikut :

MongoDB shell version v4.4.2

connecting to: mongodb://127.0.0.1:27017/test?compressors=disabled&gssapiServiceName=mongodb

Implicit session: session { "id" : UUID("5883ff70-868d-44e5-bb95-704f31348a53") }

MongoDB server version: 4.4.2

Welcome to the MongoDB shell.

For interactive help, type "help".

For more comprehensive documentation, see

        https://docs.mongodb.com/

Questions? Try the MongoDB Developer Community Forums

        https://community.mongodb.com

---

The server generated these startup warnings when booting:

        2016-11-26T18:40:05.122+07:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted

---

---

        Enable MongoDB's free cloud-based monitoring service, which will then receive and display

        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

 

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you

        and anyone you share the URL with. MongoDB may use this information to make product

        improvements and to suggest MongoDB products and deployment options to you.

 

        To enable free monitoring, run the following command: db.enableFreeMonitoring()

        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()

---

 

Buat database perusahaan dengan perintah :

> use perusahaan;

switched to db perusahaan

 

Buat pegawai dengan perintah :

> db.createCollection("pegawai");

{ "ok" : 1 }

 

Insert data ke pegawai dengan perintah :

> db.pegawai.insert({nama:"Adi Nugroho",tgl_lahir:"1990-10-10",sex:"L",gaji:"6000000"});

WriteResult({ "nInserted" : 1 })

> db.pegawai.insert({nama:"Puspa Kumalasari",tgl_lahir:"1994-11-11",sex:"P",gaji:"5500000"});

WriteResult({ "nInserted" : 1 })

 

Tampilkan data pegawai dengan perintah :

> db.pegawai.find()

{ "_id" : ObjectId("5fbfaec83460dadd2fdb3250"), "nama" : "Adi Nugroho", "tgl_lahir" : "1990-10-10", "sex" : "L", "gaji" : "6000000" }

{ "_id" : ObjectId("5fbfaf093460dadd2fdb3251"), "nama" : "Puspa Kumalasari", "tgl_lahir" : "1994-11-11", "sex" : "P", "gaji" : "5500000" }

 

Tampilkan data pegawai dengan rapi, pakai perintah :

> db.pegawai.find().pretty()

{

        "_id" : ObjectId("5fbfafc13460dadd2fdb3253"),

        "nama" : "Adi Nugroho",

        "tgl_lahir" : "1990-10-10",

        "sex" : "L",

        "gaji" : "6000000"

}

{

        "_id" : ObjectId("5fbfafc73460dadd2fdb3254"),

        "nama" : "Puspa Kumalasari",

        "tgl_lahir" : "1994-11-11",

        "sex" : "P",

        "gaji" : "5500000"

}

 

Hapus pegawai dengan perintah :

> db.pegawai.drop();

True

 

Hapus database perusahaan dengan perintah :

> db.dropDatabase();

{ "dropped" : "perusahaan", "ok" : 1 }

 

Sekian yang dapat disampaikan kali ini, silakan di eksplorasi lebih lanjut dengan perintah lainnya.