Weitere ähnliche Inhalte Ähnlich wie MongoDB Aggregation Framework in action ! (20) Mehr von Sébastien Prunier (12) Kürzlich hochgeladen (20) MongoDB Aggregation Framework in action !10. Opérateurs vs. SQL
SQL Terms, Functions, and Concepts MongoDB Aggregation Operators
WHERE $match
GROUP BY $group
HAVING $match
SELECT $project
ORDER BY $sort
LIMIT $limit
SUM() $sum
COUNT() $sum
join N/A
~ $unwind / arrays
12. Campings
{
"_id" : ObjectId("54bd347880d46d750f7a48c2"),
"raking_date" : ISODate("2013-06-06T22:00:00Z"),
"publication_date" : ISODate("2013-06-06T22:00:00Z"),
"typology" : "CAMPING",
"ranking" : "2 étoiles",
"category" : "Tourisme",
"mention" : "-",
"commercial_name" : "CAMPING LA BERGERIE",
"address" : "4231 route de Giens",
"zip_code" : "83400",
"city" : "HYÈRES",
"phone" : "494589175",
"email" : "info@camping-de-la-bergerie.com",
"website" : "www.camping-de-la-bergerie.com",
"visit_type" : "-",
"capacity" : "-",
"number_of_rooms" : "-",
"number_of_spaces" : "55",
"number_of_housing_units" : "-",
"number_of_chambers" : "-"
}
data.gouv.fr
14. Campings
db.campings.aggregate([
{$group : {_id : "$ranking", total : {$sum : 1}}}
])
{ "_id" : "5 étoiles", "total" : 177 }
{ "_id" : "4 étoiles", "total" : 940 }
{ "_id" : "3 étoiles", "total" : 2224 }
{ "_id" : "2 étoiles", "total" : 1681 }
{ "_id" : "1 étoile", "total" : 389 }
15. Campings
“ Top 5 des villes avec le plus de
campings ”
$group
$sort $limit
$project
16. Campings
db.campings.aggregate([
{$group : {_id : "$city", total : {$sum : 1}}},
{$sort : {total : -1}},
{$limit : 5},
{$project: {_id: 0, city : "$_id", total: 1}}
])
{ "city" : "ARGELÈS-SUR-MER" , "total" : 29 }
{ "city" : "AGDE", "total" : 23 }
{ "city" : "VIAS", "total" : 20 }
{ "city" : "SAINT-JEAN-DE-MONTS" , "total" : 20 }
{ "city" : "LES MATHES", "total" : 17 }
19. Marvel
{
"_id" : 4,
"title" : "Rogue (2004) #5",
"description" : "...",
"format" : "Comic",
"creators" : {
"available" : 6,
"items" : [...]
},
"characters" : {
"available" : 1,
"items" : [
{
"name" : "Rogue"
}
]
},
...
}
developer.marvel.com
20. Marvel
“ Top 5 des personnages apparaissant
dans le plus de bandes dessinées ”
$match $project
$unwind
$group
$sort $limit
21. Marvel
“Deconstructs an array field from the input documents to output a
document for each element.”
$unwind
{
"name" : "john doe",
"tags" : ["A", "B", "C"]
}
{
"name" : "john doe",
"tags" : "A"
}
{
"name" : "john doe",
"tags" : "B"
}
{
"name" : "john doe",
"tags" : "C"
}
$unwind
22. Marvel
db.comics.aggregate([
{$match : {"characters.returned" : {$gt : 0}}},
{$project : {title : 1, characters : 1}},
{$unwind : "$characters.items"},
{$group : {_id : "$characters.items.name", total : {$sum : 1}}},
{$sort : {total : -1}},
{$limit : 5}
])
{"_id": "Spider-Man","total": 2413}
{"_id": "X-Men","total": 2320}
{"_id": "Iron Man","total": 1904}
{"_id": "Wolverine","total": 1594}
{"_id": "Captain America" ,"total": 1367}
23. Marvel
“ Créez la collection des personnages à
partir de la collections des bandes
dessinées ”
$match $project
$unwind $group
$out
27. Geeks
“ Rechercher ce qui rassemble le plus
de geeks autour de Paris ”
$geoNear
$project $unwind
$group $match
$sort
29. Geeks
{
"_id" : "java",
"total" : 8,
"friends" : [
{
"nom" : "...",
"distance" : 3.015741215085397
},
{
"nom" : "...",
"distance" : 9864.484291991206
},
...
]
}
30. Index
● Index utilisé pour certains opérateurs, en
début de pipeline
○ $match
○ $sort
○ $geoNear
● explain
○ db.collection.aggregate([...],
{explain: true})
31. Index
● Optimisations
{ $sort: { age : -1 } },
{ $match: { status: 'A' } }
{ $match: { status: 'A' } },
{ $sort: { age : -1 } }
“ Optimizations are subject to change between releases. “
32. Sharding
● Découpage du pipeline en deux parties
○ 1ère partie (jusqu’au premier $group ou $sort)
exécutée sur chaque shard (certains shards peuvent
être exclus grâce à un $match)
○ 2ème partie exécutée sur le shard primaire à partir
des résultats consolidés de la première partie