Part2 Mongodb Amikom
Part2 Mongodb Amikom
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}
{a:1, b:-1}
{a.b: 1}
{a: text}
{a: 2dsphere}
{a: hashed}
deskripsi
{unique: true}
{background: true}
{name: foo}
{sparse: true}
{expireAfterSeconds:360}
{default_language: portuguese}
Illustration 3: Subdocument
2. Query Document
- Regular Expression
SQL
Like
MongoDB
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 } }
])
- Limit record
Limit
limit()
Distinct
distinct()
Contoh
Gambar 1.6
Gambar 1.7