0% found this document useful (0 votes)
2 views11 pages

TP_4_Mongo_DB

The document contains a series of MongoDB aggregation queries designed to analyze restaurant data, focusing on scores, grades, and counts based on various criteria such as cuisine and borough. Each query demonstrates how to calculate averages, maximums, minimums, and counts, as well as how to filter and project specific fields. The queries are structured to provide insights into restaurant performance and characteristics based on their grades and other attributes.

Uploaded by

saidos.isco22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views11 pages

TP_4_Mongo_DB

The document contains a series of MongoDB aggregation queries designed to analyze restaurant data, focusing on scores, grades, and counts based on various criteria such as cuisine and borough. Each query demonstrates how to calculate averages, maximums, minimums, and counts, as well as how to filter and project specific fields. The queries are structured to provide insights into restaurant performance and characteristics based on their grades and other attributes.

Uploaded by

saidos.isco22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Travaux Pratique IV : Aggregation framework-Seance2

Réaliser par :
Saïd Ali Abdi

Encadrer par :

2024 - 2025
1.Write a MongoDB query to find the average score for each restaurant.

db.restaurant.aggregate([

{ $unwind: "$grades" },

{
$group: {
_id: "$name",
moyenne_score: { $avg: "$grades.score" }
}
},

{ $sort: { moyenne_score: -1 } }
])

2. Write a MongoDB query to find the highest score for each restaurant.

db.restaurant.aggregate([

{ $unwind: "$grades" },

{
$group: {
_id: "$name",
meilleur_score: { $max: "$grades.score" }
}
},

{ $sort: { meilleur_score: -1 } }
])

3. Write a MongoDB query to find the lowest score for each restaurant.

db.restaurant.aggregate([

{ $unwind: "$grades" },

{
$group: {
_id: "$name",
minimum_score: { $min: "$grades.score" }
}
}
])
4. Write a MongoDB query to find the count of restaurants in each borough.

db.restaurant.aggregate([
{
$group: {
_id: "$borough",
nombre_restaurants: { $sum: 1 }
}
}
])
5. Write a MongoDB query to find the count of restaurants for each cuisine.

db.restaurant.aggregate([
{
$group: {
_id: "$cuisine",
nombre_restaurants: { $sum: 1 }
}
}
])
6. Write a MongoDB query to find the count of restaurants for each cuisine and borough.

db.restaurant.aggregate([
{
$group: {
_id: { cuisine: "$cuisine", borough: "$borough" },
nombre_restaurants: { $sum: 1 }
}
},
{
$sort: {
"_id.borough": 1,
"_id.cuisine": 1
}
}
])

7. Write a MongoDB query to find the count of restaurants that received a grade of 'A' for
each cuisine.

db.restaurant.aggregate([

{ $unwind: "$grades" },

{ $match: { "grades.grade": "A" } },

{
$group: {
_id: "$cuisine",
nombre_restaurants_grade_A: { $sum: 1 }
}
},

{ $sort: { nombre_restaurants_grade_A: -1 } }
])
8. Write a MongoDB query to find the count of restaurants that received a grade of 'A' for
each borough.

db.restaurant.aggregate([

{ $unwind: "$grades" },

{ $match: { "grades.grade": "A" } },

{
$group: {
_id: "$borough",
nombre_grade_A: { $sum: 1 }
}
},

{ $sort: { nombre_grade_A: -1 } }
])

9. Write a MongoDB query to find the count of restaurants that received a grade of 'A' for
each cuisine and borough.

db.restaurant.aggregate([

{ $unwind: "$grades" },

{ $match: { "grades.grade": "A" } },

{
$group: {
_id: "$restaurant_id",
cuisine: { $first: "$cuisine" },
borough: { $first: "$borough" }
}
},

{
$group: {
_id: { cuisine: "$cuisine", borough: "$borough" },
nombre_restaurants_grade_A: { $sum: 1 }
}
},

{
$sort: {
"_id.borough": 1,
"_id.cuisine": 1
}
}
])
10. Write a MongoDB query to find the number of restaurants that have been graded in each
month of the year.

db.restaurant.aggregate([
{ $unwind: "$grades" },
{
$addFields: {
mois: { $month: "$grades.date" },
annee: { $year: "$grades.date" }
}
},
{
$group: {
_id: {
restaurant_id: "$restaurant_id",
annee: "$annee",
mois: "$mois"
}
}
},
{
$group: {
_id: { annee: "$_id.annee", mois: "$_id.mois" },
nombre_restaurants_inspectes: { $sum: 1 }
}
},

{
$sort: {
"_id.annee": 1,
"_id.mois": 1
}
}
])

11. Write a MongoDB query to find the average score for each cuisine.

db.restaurant.aggregate([
{ $unwind: "$grades" },
{
$group: {
_id: "$cuisine",
score_moyen: { $avg: "$grades.score" }
}
},
{
$sort: { score_moyen: 1 }
}
])
12. Write a MongoDB query to find the highest score for each cuisine.

db.restaurant.aggregate([
{ $unwind: "$grades" },
{
$group: {
_id: "$cuisine",
max_score: { $max: "$grades.score" }
}
},

{
$sort: { max_score: -1 }
}
])

13. Write a MongoDB query to find the lowest score for each cuisine.

db.restaurant.aggregate([
{ $unwind: "$grades" },

{
$group: {
_id: "$cuisine",
min_score: { $min: "$grades.score" }
}
},

{
$sort: { min_score: 1 }
}
])

14. Write a MongoDB query to find the average score for each borough.

db.restaurant.aggregate([
{ $unwind: "$grades" },

{
$group: {
_id: "$borough",
average_score: { $avg: "$grades.score" }
}
},
{
$sort: { average_score: 1 }
}
])

15. Write a MongoDB query to find the highest score for each borough.

db.restaurant.aggregate([
{ $unwind: "$grades" },

{
$group: {
_id: "$borough",
max_score: { $max: "$grades.score" }
}
},
{
$sort: { max_score: 1 }
}
])

16. Write a MongoDB query to find the lowest score for each borough.

db.restaurant.aggregate([
{ $unwind: "$grades" },

{
$group: {
_id: "$borough",
min_score: { $min: "$grades.score" }
}
},
{
$sort: { min_score: 1 }
}
])

17. Write a MongoDB query to find the name and address of the restaurants that received a
grade of 'A' on a specific date.

db.restaurant.aggregate([
{ $unwind: "$grades" },

{
$match: {
"grades.grade": "A",
"grades.date": ISODate("2012-07-12T00:00:00Z")
}
},

{
$project: {
_id: 0,
nom: "$name",
adresse: "$address"
}
}
])

18. Write a MongoDB query to find the name and address of the restaurants that received a
grade of 'B' or 'C' on a specific date.

db.restaurant.aggregate([

{ $unwind: "$grades" },

{
$match: {
"grades.grade": { $in: ["B", "C"] },
"grades.date": ISODate("2012-07-12T00:00:00Z")
}
},

{
$project: {
_id: 0,
nom: "$name",
adresse: "$address"
}
}
])

19. Write a MongoDB query to find the name and address of the restaurants that have at
least one 'A' grade and one 'B' grade.

db.restaurant.aggregate([
{ $match: { $expr: { $and: [
{ $gt: [ { $size: { $filter: { input: "$grades", cond: { $eq: ["$$this.grade", "A"] } } } },
0 ] },
{ $gt: [ { $size: { $filter: { input: "$grades", cond: { $eq: ["$$this.grade", "B"] } } } },
0]}
] } } },
{ $project: { _id: 0, nom: "$name", adresse: "$address" } }
])

20. Write a MongoDB query to find the name and address of the restaurants that have at
least one 'A' grade and no 'B' grades.

db.restaurant.aggregate([
{ $match: { $expr: { $and: [
{ $gt: [ { $size: { $filter: { input: "$grades", cond: { $eq: ["$$this.grade", "A"] } } } },
0 ] },
{ $eq: [ { $size: { $filter: { input: "$grades", cond: { $eq: ["$$this.grade", "B"] } } } },
0]}
] } } },
{ $project: { _id: 0, nom: "$name", adresse: "$address" } }
])

21. Write a MongoDB query to find the name ,address and grades of the restaurants that
have at least one 'A' grade and no 'C' grades.

db.restaurant.aggregate([
{ $match: { $expr: { $and: [
{ $gt: [ { $size: { $filter: { input: "$grades", cond: { $eq: ["$$this.grade", "A"] } } } },
0 ] },
{ $eq: [ { $size: { $filter: { input: "$grades", cond: { $eq: ["$$this.grade", "C"] } } } },
0]}
] } } },
{ $project: { _id: 0, nom: "$name", adresse: "$address", notes: "$grades" } }
])

22. Write a MongoDB query to find the name, address, and grades of the restaurants that
have at least one 'A' grade, no 'B' grades, and no 'C' grades.

db.restaurant.aggregate([
{ $match: { $expr: { $and: [
{ $gt: [ { $size: { $filter: { input: "$grades", cond: { $eq: ["$$this.grade", "A"] } } } },
0 ] },
{ $eq: [ { $size: { $filter: { input: "$grades", cond: { $in: ["$$this.grade", ["B",
"C"]] } } } }, 0 ] }
] } } },
{ $project: { _id: 0, nom: "$name", adresse: "$address", notes: "$grades" } }
])

23. Write a MongoDB query to find the name and address of the restaurants that have the
word 'coffee' in their name.

db.restaurant.find(
{ name: { $regex: /coffee/i } },
{ _id: 0, nom: "$name", adresse: "$address" }
)
24. Write a MongoDB query to find the name and address of the restaurants that have a
zipcode that starts with '10'.

db.restaurant.find(
{ "address.zipcode": { $regex: /^10/ } },
{ _id: 0, nom: "$name", adresse: "$address" }
)
25. Write a MongoDB query to find the name and address of the restaurants that have a
cuisine that starts with the letter 'B'.

db.restaurant.find(
{ cuisine: { $regex: /^B/ } },
{ _id: 0, nom: "$name", adresse: "$address" }
)
26. Write a MongoDB query to find the name, address, and cuisine of the restaurants that
have a cuisine that ends with the letter 'y'.

db.restaurant.find(
{ cuisine: { $regex: /y$/ } },
{ _id: 0, nom: "$name", adresse: "$address", cuisine: 1 }
)
27. Write a MongoDB query to find the name, address, and cuisine of the restaurants that
have a cuisine that contains the word 'Pizza'.
db.restaurant.find(
{ cuisine: { $regex: /Pizza/i } },
{ _id: 0, nom: "$name", adresse: "$address", cuisine: 1 }
)

28. Write a MongoDB query to find the restaurants achieved highest average score.

db.restaurant.aggregate([
{ $unwind: "$grades" },
{ $group: { _id: "$name", adresse: { $first: "$address" }, avgScore: { $avg:
"$grades.score" } } },
{ $sort: { avgScore: -1 } },
{ $limit: 1 }
])
29. Write a MongoDB query to find all the restaurants with the highest number of "A" grades.

db.restaurant.aggregate([
{ $unwind: "$grades" },
{ $match: { "grades.grade": "A" } },
{ $group: { _id: "$name", adresse: { $first: "$address" }, countA: { $sum: 1 } } },
{ $sort: { countA: -1 } },
{ $limit: 1 }
])
30. Write a MongoDB query to find the cuisine type that is most likely to receive a "C" grade.

db.restaurant.aggregate([
{ $unwind: "$grades" },
{ $match: { "grades.grade": "C" } },
{ $group: { _id: "$cuisine", totalC: { $sum: 1 } } },
{ $sort: { totalC: -1 } },
{ $limit: 1 }
])
31. Write a MongoDB query to find the restaurant that has the highest average score for
thecuisine "Turkish".

db.restaurant.aggregate([
{ $match: { cuisine: "Turkish" } },
{ $unwind: "$grades" },
{ $group: { _id: "$name", adresse: { $first: "$address" }, avgScore: { $avg:
"$grades.score" } } },
{ $sort: { avgScore: -1 } },
{ $limit: 1 }
])
32. Write a MongoDB query to find the restaurants that achieved the highest total score.

db.restaurant.aggregate([
{ $unwind: "$grades" },
{ $group: { _id: "$name", adresse: { $first: "$address" }, totalScore: { $sum:
"$grades.score" } } },
{ $sort: { totalScore: -1 } },
{ $limit: 1 }
])
33. Write a MongoDB query to find all the Chinese restaurants in Brooklyn.
db.restaurant.find(
{ cuisine: "Chinese", "borough": "Brooklyn" },
{ _id: 0, nom: "$name", adresse: "$address" }
)
34. Write a MongoDB query to find the restaurant with the most recent grade date.

db.restaurant.aggregate([
{ $unwind: "$grades" },
{ $sort: { "grades.date": -1 } },
{ $limit: 1 },
{ $project: { _id: 0, nom: "$name", adresse: "$address", derniereNote: "$grades" } }
])

You might also like