Répondre aux questions suivantes
import pymongo
con = pymongo.MongoClient("mongodb://193.51.82.104:2343/")
db = con.gym
list(db.Sportifs.find({"Age": { "$gte": 20, "$lte": 30 }},
{"_id": 0, "IdSportif": 1, "Nom": 1, "Prenom": 1}))
list(db.Gymnases.find(
{
"Ville": { "$in": [ "VILLETANEUSE", "SARCELLES"]},
"Surface": { "$gt": 400 }
},
{
"_id": 0,
"NomGymnase": 1,
"Ville": 1,
"Surface": 1
}))
list(db.Sportifs.find(
{
"Sports.Jouer": "Hand ball"
},
{
"_id": 0,
"IdSportif": 1,
"Nom": 1
}
))
list(db.Gymnases.find(
{
"Seances.Libelle": "Hand ball"
},
{
"_id": 0,
"NomGymnase": 1,
"Ville": 1,
"Seances.Jour": 1,
"Seances.Libelle": 1
}
))
# **Problème** d'affichage des jours
list(db.Gymnases.aggregate([
{ "$unwind" : "$Seances" },
{ "$match": { "Seances.Libelle" : "Hand ball" }},
{ "$group": {
"_id": {
"Nom": "$Gymnase",
"Ville": "$Ville",
"Jour": { "$toLower": "$Seances.Jour"}
},
"nb": { "$sum": 1 }
}},
{ "$sort": {
"_id.Ville": 1,
"_id.Nom": 1,
"nb": -1
}}
]))
list(db.Gymnases.find(
{
"Seances.Libelle": "Hockey",
"Seances.Jour" : { "$in": [ "mercredi", "Mercredi" ]},
"Seances.Horaire": { "$gte" : 15 }
},
{
"_id": 0,
"NomGymnase": 1,
"Ville": 1,
"Seances.Jour": 1,
"Seances.Libelle": 1,
"Seances.Horaire": 1
}
))
# **Problème** de sélection (visible si on fait `$lte`)
list(db.Gymnases.aggregate([
{ "$unwind": "$Seances" },
{ "$match": {
"Seances.Libelle" : "Hockey",
"Seances.Jour": { "$in": [ "mercredi", "Mercredi" ]},
"Seances.Horaire": { "$gte": 15 }
}},
{ "$project": {
"_id": 0,
"Gymnase" : "$NomGymnase",
"Ville" : "$Ville"
}},
{ "$sort": {
"Ville": 1,
"Gymnase": 1
}}
]))
list(db.Sportifs.find(
{
"Sports" : { "$exists" : False }
},
{
"_id": 0,
"Nom": 1
}
))
list(db.Gymnases.find(
{
"Seances.Jour" : { "$nin" : [ "dimanche", "Dimanche" ]}
},
{
"_id": 0,
"NomGymnase": 1,
"Ville": 1,
"Seances.Jour": 1
}
))
list(db.Gymnases.find(
{
"$nor": [
{ "Seances.Libelle": { "$ne": "Basket ball" }},
{ "Seances.Libelle": { "$ne": "Volley ball" }}
]
},
{
"_id": 0,
"NomGymnase": 1,
"Ville": 1,
"Seances.Libelle": 1
}
))
list(db.Sportifs.find(
{
"Sports.Jouer" : { "$exists" : True },
"Sports.Entrainer" : { "$exists" : True }
},
{
"_id": 0,
"Nom": 1
}
))
list(db.Sportifs.find(
{
"IdSportif": { "$in": db.Sportifs.distinct("IdSportifConseiller")}
},
{
"_id": 0,
"Nom": 1
}
))
list(db.Sportifs.find(
{
"IdSportif": list(db.Sportifs.find({ "Nom": "KERVADEC" }))[0]["IdSportifConseiller"]
},
{
"_id": 0,
"Sports": 0
}
))
list(db.Sportifs.find(
{
"Sports.Entrainer": "Hand ball",
"Sports.Entrainer": "Basket ball"
},
{
"_id": 0,
"Nom": 1,
"Sports.Entrainer": 1
}
))
list(db.Sportifs.find(
{
"$and": [
{ "Sports.Entrainer": "Hand ball" },
{ "Sports.Entrainer": "Basket ball" }
]
},
{
"_id": 0,
"Nom": 1,
"Sports.Entrainer": 1
}
))
NE PAS FAIRE
list(db.Sportifs.aggregate([
{ "$match": { "Sports.Jouer": "Basket ball", "Sexe": { "$in": [ "f", "F" ]} }},
{ "$group": { "_id": None, "AgeMoyen": { "$avg": "$Age" }}}
]))
# On stocke le résultat de l'aggrégation dans une variable.
agemin = list(db.Sportifs.aggregate([
{ "$group": { "_id": None, "agemin": { "$min": "$Age" } } }
]))[0]
agemin
# On cherche les sportifs avec `Age = agemin.agemin`.
list(db.Sportifs.find(
{
"Age": agemin["agemin"]
},
{
"_id": 0,
"Nom": 1,
"Age": 1
}
))
# On peut chercher les plus grands gymnases de chaque ville.
grand = list(db.Gymnases.aggregate([
{ "$group": { "_id": "$Ville", "surfmax": { "$max": "$Surface" }} }
]))
grand
# Ensuite, on cherche comme ci-dessus
# On récupère la liste des sports entraînés.
sports = db.Sportifs.distinct("Sports.Entrainer")
sports
# On fait un filtre pour supprimer le hand et le basket.
autres = list(filter(lambda s: (s != "Hand ball") & (s != "Basket ball"), sports))
autres
# On cherche ceux qui entraîne l'un ou l'autre, mais pas d'autres sports.
list(db.Sportifs.find(
{
"$and": [
{ "$or": [
{ "Sports.Entrainer": "Hand ball" },
{ "Sports.Entrainer": "Basket ball" }
] },
{ "Sports.Entrainer" : { "$nin" : autres }}
]
},
{
"_id": 0,
"Nom": 1,
"Sports.Entrainer": 1
}
))
list(db.Sportifs.find(
{
"IdSportifConseiller": { "$exists": False }
},
{
"_id": 0,
"Nom": 1
}
))
list(db.Sportifs.aggregate([
{ "$match": { "Sports.Arbitrer": { "$exists": True }}},
{ "$unwind": "$Sports.Arbitrer"},
{ "$group": { "_id": "$Nom", "nbArbitrer": { "$sum": 1 }}}
]))
list(db.Gymnases.aggregate([
{ "$match": { "Ville": "STAINS" } },
{ "$unwind": "$Seances" },
{ "$project": { "nom": "$NomGymnase", "jour": { "$toLower": "$Seances.Jour" }, "h": "$Seances.Horaire"}},
{ "$group": { "_id": { "nom": "$nom", "jour": "$jour" }, "debut": { "$min": "$h"}, "fin": { "$max": "$h" }}}
]))
# On récupère la liste des identifiants des entraîneurs de Hand.
entraineursHand = list(db.Sportifs.find(
{ "Sports.Entrainer" : "Hand ball" },
{ "_id": 0, "IdSportif": 1 }
))
[e["IdSportif"] for e in entraineursHand]
# On les utilise pour faire la restriction (avec le `$match`) dans l'aggrégation.
list(db.Gymnases.aggregate([
{ "$unwind": "$Seances" },
{ "$match": { "Seances.IdSportifEntraineur": { "$in": [e["IdSportif"] for e in entraineursHand] }}},
{ "$project" : { "ent": "$Seances.IdSportifEntraineur", "jour": { "$toLower": "$Seances.Jour"}}},
{ "$group": { "_id": { "entraineur": "$ent", "jour": "$jour"}, "nbSeances": { "$sum": 1}}}
]))
# A priori, il n'y en a aucun.
list(db.Gymnases.aggregate([
{ "$unwind": "$Seances" },
{ "$match": { "Seances.Jour": { "$in": [ "mercredi", "Mercredi" ] }}},
{ "$group": { "_id": { "nom": "$NomGymnase", "ville": "$Ville" }, "nbMercredi": { "$sum": 1 }}},
{ "$match": { "nbMercredi": { "$gte": 15 }}}
]))