0% found this document useful (1 vote)
89 views

MongoDB Advanced Query, Array, Aggregation

The document describes 9 examples of aggregation queries on a books database collection to match, project, group, lookup, unwind, sort, and limit data from related authors and publishers collections. The examples demonstrate matching documents by field, projecting fields, summing field values, joining related data, grouping and counting documents, sorting results, and limiting the number of documents returned.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
89 views

MongoDB Advanced Query, Array, Aggregation

The document describes 9 examples of aggregation queries on a books database collection to match, project, group, lookup, unwind, sort, and limit data from related authors and publishers collections. The examples demonstrate matching documents by field, projecting fields, summing field values, joining related data, grouping and counting documents, sorting results, and limiting the number of documents returned.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Gabungkan (menampilkan) data buku dari author id 1 dan author id 2


db.books.aggregate(
[
{$match: {$or: [{authorID: 1}, {authorID: 2}]}}
]
)

2. Tampilkan daftar buku dan harga author id 1


db.books.aggregate(
[
{$match: {authorID: 1}},
{$project: {title: 1, price: 1}}
]
)

3. Tampilan total jumlah halaman buku author id 2


db.books.aggregate(
[
{$match: {authorID: 2}},
{$group:
{
_id: "$authorID",
totalPages: {$sum: "$stats.page"}
}
}
]
)

4. Tampilkan semua field books and authors terkait


db.authors.aggregate([
{
$lookup:
{
from: "books",
localField: "_id",
foreignField: "authorID",
as: "books"
}
}
])

5. Tampilkan semua field books, authors, dan publishers terkait.


db.books.aggregate([
{
$lookup:
{
from: "authors",
localField: "authorID",
foreignField: "_id",
as: "authors"
}
},
{
$lookup:
{
from: "publishers",
localField: "publisherID",
foreignField: "_id",
as: "publishers"
}
}
])

6. Tampilkan summary data authors, books, dan publishers sesuai dengan Output.
db.books.aggregate([
{
$lookup: {
from: "authors",
localField: "authorID",
foreignField: "_id",
as: "authors"
}
},
{
$lookup: {
from: "publishers",
localField: "publisherID",
foreignField: "_id",
as: "publishers"
}
},
{$unwind: "$publishers"},
{$unwind: "$authors"},
{
$group: {
_id: {$concat: ["$authors.firstName", " ", "$authors.lastName"]},
number_of_books: {$sum: 1},
list_of_books: {$push: {$concat: ["$title", "(", "$publishers.publisherName",
")"]}}
}
},
{
$sort: {number_of_books: 1}
}
])

7. Digital_outlet ingin memberikan diskon untuk setiap buku, diskon di tentukan


melihat harga buku tersebut dengan pembagian seperti ini
db.books.aggregate([
{
$project: {
_id: "$_id",
title: "$title",
price: "$price",
promo: {
$switch: {
branches: [
{case: {$lt: ["$price", 60000]}, then: "1%"},
{case: {$lt: ["$price", 90000]}, then: "2%"},
{case: {$gt: ["$price", 90000]}, then: "3%"}
]
}
}
}
}])

8. Tampilkan semua nama buku, harga, nama author dan nama publisher, urutkan dari
harga termahal ke termurah
db.books.aggregate([
{
$lookup: {
from: "authors",
localField: "authorID",
foreignField: "_id",
as: "authors"
}
},{
$lookup: {
from: "publishers",
localField: "publisherID",
foreignField: "_id",
as: "publishers"
}
},
{$unwind: "$authors"},
{$unwind: "$publishers"},
{
$project: {
_id: 1,
tittle: 1,
price: 1,
author: {$concat : ["$authors.firstName", " ", "$authors.lastName"]},
publisher: "$publishers.publisherName"
}
},
{
$sort: {price: -1}
}])

9. Tampilkan data nama buku harga dan publisher, kemudian tampilkan hanya data ke 3
dan ke 4.
db.books.aggregate([
{
$lookup: {
from: "authors",
localField: "authorID",
foreignField: "_id",
as: "authors"
}
},{
$lookup: {
from: "publishers",
localField: "publisherID",
foreignField: "_id",
as: "publishers"
}
},
{$unwind: "$authors"},
{$unwind: "$publishers"},
{
$project: {
_id: 1,
tittle: 1,
price: 1,
publisher: "$publishers.publisherName"
}
},
{
$skip: 2
},{
$limit: 2
}
])

You might also like