2)
1. Toutes les informations des restaurants dans Manhattan:
db.resto.find({"borough":"Manhattan"})
detail = {$match:{borough:"Manhattan"}}
db.resto.aggragate([detail])
2. Cherchon ceux qui font la cuisine marocaire
db.resto.find({"borough":"Manhattan", "cuisine":"Morrocan"})
detail2 = {$match:{cuisine:"Moroccan"}}
db.resto.aggregate([detail, detail2])
3.
db.resto.find({"borough":"Manhattan", "cuisine":"Moroccan"}, {name:1,_id:0})
name={$project:{name:1,_id:0}}
resto> db.resto.aggregate([detail, detail2,name])
4.
db.resto.find({"borough":"Manhattan", "cuisine":"Seafood"}, {name:1,_id:0})
detail22 = {$match:{cuisine:"Seafood"}}
db.resto.aggregate([detail, detail22,name])
5.
db.resto.find({"borough":"Manhattan", "cuisine":"Italian", "name":
/pizza/i,"grades.score":{$lt:10,$not:{$gte:10}}}, {name:1})
db.resto.aggregate([ { $match: { borough: "Manhattan", cuisine: "Italian",
name: { $regex: /pizza/i }, "grades.score": { $lt: 10, $not: { $gte: 10 } } } },
{ $project: { _id: 0, name: 1 } }] );
6. db.resto.find( { grades: { $elemMatch: { grade: "C", score: { $lt: 30 } } } }, {
name: 1, grades: 1, _id: 0 } );
db.resto.aggregate([ { $match: { grades: { $elemMatch: { grade: "C", score:
{ $lt: 30 } } } } }, { $project: { _id: 0, name: 1, grades: 1 } }] );
db.resto.find( { "grades.0.grade": "C" }, { name: 1, borough: 1, _id: 0 } );
db.resto.aggregate([ { $match: { "grades.0.grade": "C" } }, { $project:
{ _id: 0, name: 1, borough: 1 } }] );
3)
Différentes spécialités (cuisines) des resto :
db.resto.distinct("cuisine");
db.resto.aggregate([ { $group: { _id: "$cuisine" } }, { $project: { _id: 0,
cuisine: "$_id" } }] );
Différents grades de notation des resto :
db.resto.distinct("grades.grade");
db.resto.aggregate([ { $unwind: "$grades" }, { $group: { _id: "$grades.grade" } },
{ $project: { _id: 0, grade: "$_id" } }] );
4)
1.
db.resto.find({ "grades.0.grade":"C" }, {"name":1, "borough":1, "_id":0} )
db.resto.aggregate({$match : {"grades.0.grade" : "C"}},{$project : {"name" :
1, "borough" : 1, "_id" : 0}})
2. Utilisation des variables
Match = { $match : { "grades.0.grade":"C"} };
Project = { $project : {"name":1, "borough":1, "_id":0}};
db.resto.aggregate( [ Match, Project ] );
3. Le trie
trie = { $sort : {"name" : 1}};
db.resto.aggregate( [ Match, Project, trie] );
4. Groupement simple
Groupe = { $group : {"_id" : null, "total" : {$sum : 1} } };
db.resto.aggregate( [ Match, Groupe ] );
db.resto.count({"grades.0.grade":"C"})
ou
db.resto.find({"grades.0.grade":"C"}).count()
5. Groupement par valeur
Groupe2 = { $group : {"_id" : "borough", "total" : {$sum : 1} } };
db.resto.aggregate( [ Match, Groupe2 ] );
Groupe3 = { $group : {"_id" : "$borough", "total" : {$sum : 1} } };
db.resto.aggregate( [ Match, Groupe3 ] );
Groupe3 = { $group: {"_id": "$borough", "total": {$sum: 1} } };
db.resto.aggregate( [Match, Groupe3]);
Groupe4 = { $group: {"_id": "$cuisine", "total": {$sum: 1} } }; {"$group":{"_id":
"$cuisine", "total": { "$sum": 1}}};
db.resto.aggregate( [Match, Groupe4]);
db.resto.aggregate( [Groupe4]);
6. Unwind
Unwind = {$unwind : "$grades"}
Group4 = { $group : {"_id" : "$borough", "moyenne" : {$avg :"$grades.score"} } };
Sort2 = { $sort : { "moyenne" : -1 } }
db.resto.aggregate( [ Unwind, Group4, Sort2 ] );