Anda di halaman 1dari 9

MongoDB

Advanced

1. Insert documents
1.1.
Indexing
db.<collections>.createIndex(<key and index type specification>, <options>)
Mongodb memiliki beberapa jenis index, setiap index memiliki karaktersistik
tersendiri. Tabel dibawah ini menunjukkan jenis jenis tabel yang didukung oleh
mongodb.
Jenis index

Deskripsi

{a:1}

Simple index pada field a.

{a:1, b:-1}

Index gabungan dengan a ascending dan b


descending.

{a.b: 1}

Ascending index on embedded field a.b.

{a: text}

Text index on field a. A collection can have at


most one text index.

{a: 2dsphere}

Geospatial index where the a field stores


GeoJSON data. See documentation for valid
GeoJSON formatting.

{a: hashed}

Hashed index on field a. Generally used with


hash-based sharding.

Table 1: Jenis index pada mongodb.


Mongodb index memiliki beberapa option yang dapat dimanfaatkan sesuai dengan kebutuhan.
Option index ini memiliki nilai default masing masing. Pembuatan index tetap dapat dilakukan
walaupun options tidak ditentukan.
N ilai options

deskripsi

{unique: true}

Creates an index that requires all values of the


indexed field to be unique.

{background: true}

Creates this index in the background; useful


when you need to minimize index creation
performance impact.

{name: foo}

Specifies a custom name for this index. If not


specified, the name will be derived from the key
pattern.

{sparse: true}

Creates entries in the index only for documents


having the index key.

{expireAfterSeconds:360}

Creates a time to live (TTL) index on the index


key. This will force the system to drop the
document after 3600 seconds expire. Only
works on keys of date type.

{default_language: portuguese}

Used with text indexes to define the default


language
used for stop words and stemming.

Illustration 1: Membuat index dengan option drop


duplicate
db.<collections>.createIndex(<key and index type specification>, <options>)
Contoh:
a) db.users.createIndex({
username: 1
}, {
dropDups: true
});
1.2.
Reference and subdocument
Relasi antar tabel atau collection dapat dilakukan dengan 2 cara, cara pertama yaitu
dengan cara reference, dan cara kedua adalah sub document. Kedua cara ini memiliki
kelebihan dan kekurangannya masing masing.

Illustration 2: Dua tabel yang saling berkaitan

Illustration 3: Subdocument

Illustration 4: Reference ke table lain menggunakan DBref

Illustration 5: Table alamat yang dihubungkan dengan tabel user.

Illustration 6: Insert DBref pada tabel.


a) Reference
Reference dapat dilakukan dengan DBref ataupun dengan menentukan _id rujukan
dari record pada table lain. Pada ilustrasi no 2 menunjukkan bahwa terdapat 2 tabel
yang saling berkaitan yang dihubungkan dengan menambahkan _id dari tabel lain
yang berkaitan pada field yang diinginkan. Sedangkan pada ilustrasi no 4 reference
dilakukan dengan menggunakan Dbref. Insert Dbref pada tabel dapat dilakukan
seperti yang terlihat pada ilustrasi no 6
b) Subdocument
Subdocument pada dasarnya hanyalah array of object pada salah satu field dari
document.

2. Query Document
- Regular Expression
SQL
Like

MongoDB

select * from student where Name like '%P db.Student.find({Name:


%'
{$regex:"P"}}) atau
db.Student.find({Name:/P/})
select * from student where Name like '%a' db.Student.find({Name:
{$regex:"a$"}}) atau
db.Student.find({Name:/a$/})
select * from student where Name like 'p%' db.Student.find({Name:
{$regex:"^P"}}) atau
db.Student.find({Name:/^P/})

Contoh:

Gambar 1.0
- Query operator
Operator

Syntax

<

$lt

<=

$lte

>

$gt

>=

$gte

!=

$ne

And

$and

Or

$or
$all
$in
$nin

Contoh:

Gambar 1.1

Gambar 1.2

Gambar 1.3

Gambar 1.4

Gambar 1.5
- Text Searching
MongoDB mendukung query untuk mendapatkan string dalam suatu konten.
Contoh:
1. Tambahkan data berikut:

2. Create index

3. Text operator

4. Hasil

- Aggregation
Menghitung nilai dari tumpukan record query. Misalnya ada table yang memiliki record seperti
dibawah ini :
[

{
status:
{
status:
{
status:
{
status:
{
status:
]

_id:
"A",
_id:
"A",
_id:
"D",
_id:
"D",
_id:
"A",

1, cust_id: "abc1",
amount: 50 },
2, cust_id: "xyz1",
amount: 100 },
3, cust_id: "xyz1",
amount: 25 },
4, cust_id: "xyz1",
amount: 125 },
5, cust_id: "abc1",
amount: 25 }

ord_date: ISODate("2012-11-02T17:04:11.102Z"),
ord_date: ISODate("2013-10-01T17:04:11.102Z"),
ord_date: ISODate("2013-10-12T17:04:11.102Z"),
ord_date: ISODate("2013-10-11T17:04:11.102Z"),
ord_date: ISODate("2013-11-12T17:04:11.102Z"),

Untuk mendapatkan jumlah total amount yang memiliki status A dapat dilakukan dengan query
berikut:
db.orders.aggregate([
{ $match: { status: "A" } },
{ $group: { _id: "$cust_id", total: { $sum:
"$amount" } } },

{ $sort: { total: -1 } }
])

Dari query diatas hasil yang didapatkan adalah sebagai berikut :


{ "_id" : "xyz1", "total" : 100 }
{ "_id" : "abc1", "total" : 75 }

- Limit record
Limit

limit()

Distinct

distinct()

Contoh

Gambar 1.6

Gambar 1.7

Anda mungkin juga menyukai