Monday, December 7, 2020

.::: Tutorial MongoDB, create Database & Collection (table) nosql :::.


C:\Users\teguh>cd "c:\Program Files\MongoDB\Server\4.4\bin"

Nama database : kampus
Nama collection : mahasiswi
Nama field : nim, nama, alamat
Saat membuat database gunakan perintah


c:\Program Files\MongoDB\Server\4.4\bin>mongo
MongoDB shell version v4.4.2
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("62a80aef-15de-439f-a23a-b960ce6538d2") }
MongoDB server version: 4.4.2
---
The server generated these startup warnings when booting:
        2020-12-03T10:29:18.368+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()
---
>

Contoh kasusnya adalah sama dengan kasus di artikel Database MySQL yaitu

use nama_database

cd C:\Users\teguh>cd "c:\Program Files\MongoDB\Server\4.4\bin"

A. Create & Delete Database

1. Create Database
sintax
show dbs
use kampus
db.mahasiswi.insert({"nim":"11223344", "nama":"Tila", "alamat":"bandung"})
show dbs

sintax detail
> sintax
> show dbs
> use kampus
> db.mahasiswi.insert({"nim":"11223344", "nama":"Tila", "alamat":"bandung"})
> show dbs

log
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
toko    0.000GB
>
> use kampus
switched to db kampus
> db.mahasiswi.insert({"nim":"11223344", "nama":"Tila", "alamat":"bandung"})
WriteResult({ "nInserted" : 1 })
>
> show dbs
admin   0.000GB
config  0.000GB
kampus  0.000GB
local   0.000GB
toko    0.000GB
>

2. detele database
sintax
> show dbs
> db.dropDatabase()
> show dbs

log 
> show dbs
admin   0.000GB
config  0.000GB
kampus  0.000GB
local   0.000GB
toko    0.000GB
>
> db.dropDatabase()
{ "dropped" : "kampus", "ok" : 1 }
>
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
toko    0.000GB
>

B. Management collection (Table) 
db.createCollection("nama koleksi")
1. delete collection (table)
sintax
> use kampus
> db.mahasiswi.drop()
> show collections
log
> use kampus
switched to db kampus
> db.mahasiswi.drop()
true
> show collections

2. create collection (table)
sintax
> use kampus
> db.createCollection("mahasiswi")
> show collections
log
> use kampus
switched to db kampus
> db.createCollection("mahasiswi")
{ "ok" : 1 }
> show collections
mahasiswi
>

C. Insert, Query, Update, Delete 
1. Insert
db.collection_name.insert(document)
{
    "nim":"11223344", 
    "nama":"Tila", 
    "alamat":"bandung"
}
sintax 
db.mahasiswi.insert({"nim":"11223344", "nama":"Tila", "alamat":"bandung"})
db.mahasiswi.insert([{"nim":"11223355","nama":"Tilaxx", "alamat":"bandungxx"},{"nim":"11223366","nama":"dewi","alamat":"padang","hobi": "coding"}])
log 
> db.mahasiswi.insert({"nim":"11223344", "nama":"Tila", "alamat":"bandung"})
WriteResult({ "nInserted" : 1 })
>
> db.mahasiswi.insert([
    {
        "nim":"11223355", 
        "nama":"Tilaxx", 
        "alamat":"bandungxx"
    },{
        "nim":"11223366", 
        "nama":"dewi", 
        "alamat":"padang",
        "hobi": "coding"
    }
])
>
> db.mahasiswi.insert([
...     {
...         "nim":"11223355",
...         "nama":"Tilaxx",
...         "alamat":"bandungxx"
...     },{
...         "nim":"11223366",
...         "nama":"dewi",
...         "alamat":"padang",
...         "hobi": "coding"
...     }
... ])
BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 2,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [ ]
})
>

2. query
db.collection_name.find()
db.collection_name.find().pretty()
sintax 
> db.mahasiswi.find()
> db.mahasiswi.find().pretty()

log 
> db.mahasiswi.find()
{ "_id" : ObjectId("5fc867cac0904147c6cbe4f5"), "nim" : "11223344", "nama" : "Tila", "alamat" : "bandung" }
{ "_id" : ObjectId("5fc86814c0904147c6cbe4f6"), "nim" : "11223355", "nama" : "Tilaxx", "alamat" : "bandungxx" }
{ "_id" : ObjectId("5fc86814c0904147c6cbe4f7"), "nim" : "11223366", "nama" : "dewi", "alamat" : "padang", "hobi" : "coding" }
>
> db.mahasiswi.find().pretty()
{
        "_id" : ObjectId("5fc867cac0904147c6cbe4f5"),
        "nim" : "11223344",
        "nama" : "Tila",
        "alamat" : "bandung"
}
{
        "_id" : ObjectId("5fc86814c0904147c6cbe4f6"),
        "nim" : "11223355",
        "nama" : "Tilaxx",
        "alamat" : "bandungxx"
}
{
        "_id" : ObjectId("5fc86814c0904147c6cbe4f7"),
        "nim" : "11223366",
        "nama" : "dewi",
        "alamat" : "padang",
        "hobi" : "coding"
}
>


3. update 
db.collection_name.update({nilai document lama}, {$set:{nilai document baru}})
sintax 
> db.mahasiswi.find()
> db.mahasiswi.update({"alamat":"padang"}, {$set:{"alamat":"surabaya"}})
> db.mahasiswi.find()

log 
> db.mahasiswi.find()
{ "_id" : ObjectId("5fc867cac0904147c6cbe4f5"), "nim" : "11223344", "nama" : "Tila", "alamat" : "bandung" }
{ "_id" : ObjectId("5fc86814c0904147c6cbe4f6"), "nim" : "11223355", "nama" : "Tilaxx", "alamat" : "bandungxx" }
{ "_id" : ObjectId("5fc86814c0904147c6cbe4f7"), "nim" : "11223366", "nama" : "dewi", "alamat" : "padang", "hobi" : "coding" }
>
> db.mahasiswi.update({"alamat":"padang"}, {$set:{"alamat":"surabaya"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.mahasiswi.find()
{ "_id" : ObjectId("5fc867cac0904147c6cbe4f5"), "nim" : "11223344", "nama" : "Tila", "alamat" : "bandung" }
{ "_id" : ObjectId("5fc86814c0904147c6cbe4f6"), "nim" : "11223355", "nama" : "Tilaxx", "alamat" : "bandungxx" }
{ "_id" : ObjectId("5fc86814c0904147c6cbe4f7"), "nim" : "11223366", "nama" : "dewi", "alamat" : "surabaya", "hobi" : "coding" }
>


4. delete 
db.collection_name.remove(nilai dokumen) 
sintax 
> db.mahasiswi.find()
> db.mahasiswi.remove({"alamat":"bandung"})
> db.mahasiswi.find()

log 
> db.mahasiswi.find()
{ "_id" : ObjectId("5fc867cac0904147c6cbe4f5"), "nim" : "11223344", "nama" : "Tila", "alamat" : "bandung" }
{ "_id" : ObjectId("5fc86814c0904147c6cbe4f6"), "nim" : "11223355", "nama" : "Tilaxx", "alamat" : "bandungxx" }
{ "_id" : ObjectId("5fc86814c0904147c6cbe4f7"), "nim" : "11223366", "nama" : "dewi", "alamat" : "surabaya", "hobi" : "coding" }
> db.mahasiswi.remove({"alamat":"bandung"})
WriteResult({ "nRemoved" : 1 })
> db.mahasiswi.find()
{ "_id" : ObjectId("5fc86814c0904147c6cbe4f6"), "nim" : "11223355", "nama" : "Tilaxx", "alamat" : "bandungxx" }
{ "_id" : ObjectId("5fc86814c0904147c6cbe4f7"), "nim" : "11223366", "nama" : "dewi", "alamat" : "surabaya", "hobi" : "coding" }
>


https://ngodingdata.com/tutorial-mongodb-cara-membuat-database-dan-collection/  

No comments:

Post a Comment

Popular Posts