Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Aggregate MongoDB + ElasticSearch

Ghiffari Agsarya Arlin – UI BE 02

1. Masukkan 3 buah data tentang profil

> db.profiles.insertMany([
... {
... name: "Ghiffari",
... ratings: [
... { by: "ahmad", date: "2020-10-20", rate: 9 },
... { by: "roni", date: "2020-10-06", rate: 8 },
... { by: "ilham", date: "2020-07-23", rate: 9 },
... { by: "supri", date: "2020-05-09", rate: 9 },
... { by: "fatah", date: "2020-08-18", rate: 8 },
... { by: "amir", date: "2020-11-08", rate: 9 },
... ],
... tags: ["finance", "banking", "business", "economy"],
... location: [1, 2],
... },
...
... {
... name: "Agsarya",
... ratings: [
... { by: "ahmad", date: "2019-11-09", rate: 8 },
... { by: "ilham", date: "2019-12-24", rate: 8 },
... ],
... tags: ["finance", "economy", "banking"],
... location: [3, 4],
... },
... {
... name: "Arlin",
... ratings: [
... { by: "ahmad", date: "2020-01-11", rate: 7 },
... { by: "roni", date: "2020-01-30", rate: 6 },
... { by: "surti", date: "2019-12-20", rate: 7.5 },
... { by: "fatah", date: "2020-02-02", rate: 7 },
... ],
... tags: ["finance", "business"],
... location: [4, 5],
... },
... ]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5f92e03828a29fbcfa99d440"),
ObjectId("5f92e03828a29fbcfa99d441"),
ObjectId("5f92e03828a29fbcfa99d442")
]
}

> db.locations.insertMany([
... {
... _id: 1,
... city: "Yogyakarta",
... loc: { type: "Point", coordinates: [110.370529, -7.797068] },
... },
... {
... _id: 2,
... city: "Sleman",
... loc: { type: "Point", coordinates: [110.326498694,
-7.6999972] },
... },
... {
... _id: 3,
... city: "Depok",
... loc: { type: "Point", coordinates: [106.830711, -6.385589] },
... },
... {
... _id: 4,
... city: "Bogor",
... loc: { type: "Point", coordinates: [106.816635, -6.595038] },
... },
... {
... _id: 5,
... city: "Bandung",
... loc: { type: "Point", coordinates: [107.60981, -6.914744] },
... },
... ]);
{ "acknowledged" : true, "insertedIds" : [ 1, 2, 3, 4, 5 ] }

2. Tambahkan beberapa tag ke profil

> db.profiles.update(
... { name: "Arlin" },
... { $push: { tags: { $each: ["tax", "management"] } } }
... )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.profiles.find({name:"Arlin"}).pretty();
{
"_id" : ObjectId("5f92bc9c28a29fbcfa99d43f"),
"name" : "Arlin",
"ratings" : [
{
"by" : "ahmad",
"date" : "2020-01-11",
"rate" : "7"
},
{
"by" : "roni",
"date" : "2020-01-30",
"rate" : "6"
},
{
"by" : "surti",
"date" : "2019-12-20",
"rate" : "7.5"
},
{
"by" : "fatah",
"date" : "2020-02-02",
"rate" : "7"
}
],
"tags" : [
"finance",
"business",
"tax",
"management"
],
"location" : [
4,
5
]
}
3. Tampilkan 3 tag yang paling banyak dipakai

> db.profiles.aggregate(
... { $unwind: "$tags" },
... { $group: { _id: "$tags", count: { $sum: 1 } } },
... { $sort: { count: -1 } },
... { $limit: 3 }
... );
{ "_id" : "finance", "count" : 3 }
{ "_id" : "business", "count" : 2 }
{ "_id" : "banking", "count" : 2 }

4. Tampilkan 4 orang yang paling banyak melakukan review

> db.profiles.aggregate(
... { $unwind: "$ratings" },
... { $group: { _id: "$ratings.by", count: { $sum: 1 } } },
... { $sort: { count: -1 } },
... { $limit: 4 }
... );
{ "_id" : "ahmad", "count" : 3 }
{ "_id" : "roni", "count" : 2 }
{ "_id" : "ilham", "count" : 2 }
{ "_id" : "fatah", "count" : 2 }

5. Tampilkan 3 record rating yang paling tinggi nilainya

> db.profiles.aggregate(
... { $unwind: "$ratings" },
... {
... $group: {
... _id: { by: "$ratings.by", date: "$ratings.date" },
... rate: { $max: "$ratings.rate" },
... },
... },
... { $sort: { rate: -1 } },
... { $limit: 3 }
... );
{ "_id" : { "by" : "amir", "date" : "2020-11-08" }, "rate" : 9 }
{ "_id" : { "by" : "ilham", "date" : "2020-07-23" }, "rate" : 9 }
{ "_id" : { "by" : "ahmad", "date" : "2020-10-20" }, "rate" : 9 }

- Tampilkan siapa yang dirating

6. Tampilkan semua record location yang paling dekat dengan Jakarta (106.8456, 6.2088)

> db.locations.createIndex({ loc: "2dsphere" });


{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}

> db.locations.aggregate([
... {
... $geoNear: {
... near: { type: "Point", coordinates: [106.8456, 6.2088] },
... distanceField: "dist.jarak",
... },
... },
... { $sort: { "dist.jarak": 1 } },
... ]).pretty();

{
"_id" : 3,
"city" : "Depok",
"loc" : {
"type" : "Point",
"coordinates" : [
106.830711,
-6.385589
]
},
"dist" : {
"jarak" : 1401993.8129792032
}
}
{
"_id" : 4,
"city" : "Bogor",
"loc" : {
"type" : "Point",
"coordinates" : [
106.816635,
-6.595038
]
},
"dist" : {
"jarak" : 1425312.0898477605
}
}
{
"_id" : 5,
"city" : "Bandung",
"loc" : {
"type" : "Point",
"coordinates" : [
107.60981,
-6.914744
]
},
"dist" : {
"jarak" : 1463361.675325551
}
}
{
"_id" : 2,
"city" : "Sleman",
"loc" : {
"type" : "Point",
"coordinates" : [
110.326498694,
-7.6999972
]
},
"dist" : {
"jarak" : 1595823.3297748524
}
}
{
"_id" : 1,
"city" : "Yogyakarta",
"loc" : {
"type" : "Point",
"coordinates" : [
110.370529,
-7.797068
]
},
"dist" : {
"jarak" : 1607488.5017036977
}
}

7. Tampilkan rata-rata rating dari yang tertinggi

> db.profiles.aggregate(
... { $unwind: "$ratings" },
... {
... $group: {
... _id: {
... by: "$ratings.by",
... },
... avg: { $avg: "$ratings.rate" },
... },
... },
... { $sort: { avg: -1 } }
... );

{ "_id" : { "by" : "supri" }, "avg" : 9 }


{ "_id" : { "by" : "amir" }, "avg" : 9 }
{ "_id" : { "by" : "ilham" }, "avg" : 8.5 }
{ "_id" : { "by" : "ahmad" }, "avg" : 8 }
{ "_id" : { "by" : "fatah" }, "avg" : 7.5 }
{ "_id" : { "by" : "surti" }, "avg" : 7.5 }
{ "_id" : { "by" : "roni" }, "avg" : 7 }

8. Tampilkan

You might also like