Introduction à MongoDB - correction

Répondre aux questions suivantes

In [1]:
import pymongo
con = pymongo.MongoClient("mongodb://193.51.82.104:2343/")
db = con.gym
1. Quels sont les sportifs (identifiant, nom et prénom) qui ont entre 20 et 30 ans ?
In [2]:
list(db.Sportifs.find({"Age": { "$gte": 20, "$lte": 30 }},
                      {"_id": 0, "IdSportif": 1, "Nom": 1, "Prenom": 1}))
Out[2]:
[{'IdSportif': 1, 'Nom': 'BOUTAHAR', 'Prenom': 'Abderahim'},
 {'IdSportif': 2, 'Nom': 'KERVADEC', 'Prenom': 'Yann'},
 {'IdSportif': 3, 'Nom': 'HUE', 'Prenom': 'Pascale'},
 {'IdSportif': 5, 'Nom': 'COMES', 'Prenom': 'Sylvie'},
 {'IdSportif': 6, 'Nom': 'RETALDI', 'Prenom': 'Sophie'},
 {'IdSportif': 7, 'Nom': 'GOMEZ', 'Prenom': 'Diego'},
 {'IdSportif': 9, 'Nom': 'ANTUNES', 'Prenom': 'Gaêlle'},
 {'IdSportif': 10, 'Nom': 'BLANDET', 'Prenom': 'Arnaud'},
 {'IdSportif': 11, 'Nom': 'BONNET', 'Prenom': 'Christophe'},
 {'IdSportif': 12, 'Nom': 'BORREL', 'Prenom': 'Benoît'},
 {'IdSportif': 13, 'Nom': 'BOYON', 'Prenom': 'Julien'},
 {'IdSportif': 14, 'Nom': 'CAILLIOT', 'Prenom': 'Stéphanie'},
 {'IdSportif': 15, 'Nom': 'CHAMPENOIS', 'Prenom': 'Cédric'},
 {'IdSportif': 16, 'Nom': 'COLAS', 'Prenom': 'Michaël'},
 {'IdSportif': 17, 'Nom': 'COLOMB', 'Prenom': 'Michaël'},
 {'IdSportif': 18, 'Nom': 'HOSNI', 'Prenom': 'Leila'},
 {'IdSportif': 19, 'Nom': 'TESTEMONT', 'Prenom': 'Laurent'},
 {'IdSportif': 20, 'Nom': 'TIENER', 'Prenom': 'Thomas'},
 {'IdSportif': 21, 'Nom': 'LE BOUCHER', 'Prenom': 'Denis'},
 {'IdSportif': 22, 'Nom': 'LEDUFAUD', 'Prenom': 'Pierre'},
 {'IdSportif': 23, 'Nom': 'LEGRAND', 'Prenom': 'David'},
 {'IdSportif': 24, 'Nom': 'LEJEUNE', 'Prenom': 'Sylvaine'},
 {'IdSportif': 25, 'Nom': 'MARTORA', 'Prenom': 'Fabrice'},
 {'IdSportif': 26, 'Nom': 'MICHEL', 'Prenom': 'Frédérique'},
 {'IdSportif': 27, 'Nom': 'NIELLEZ', 'Prenom': 'Christophe'},
 {'IdSportif': 29, 'Nom': 'ROUSSEL', 'Prenom': 'Nadège'},
 {'IdSportif': 30, 'Nom': 'SCHINK', 'Prenom': 'Nicolas'},
 {'IdSportif': 31, 'Nom': 'STEMPUT', 'Prenom': 'Mathieu'},
 {'IdSportif': 32, 'Nom': 'VAN CAUTER', 'Prenom': 'Vincent'},
 {'IdSportif': 33, 'Nom': 'RAMPNOUX', 'Prenom': 'Jean françois'},
 {'IdSportif': 34, 'Nom': 'LHERPINIERE', 'Prenom': 'Olivier'},
 {'IdSportif': 35, 'Nom': 'TANQUE', 'Prenom': 'Yann'},
 {'IdSportif': 36, 'Nom': 'DJELOUDANE', 'Prenom': 'Zinedine'},
 {'IdSportif': 37, 'Nom': 'LAZZARI', 'Prenom': 'Magali'},
 {'IdSportif': 38, 'Nom': 'VASSEMON', 'Prenom': 'Laurent'},
 {'IdSportif': 39, 'Nom': 'MOREL', 'Prenom': 'Mathieu'},
 {'IdSportif': 40, 'Nom': 'VALIN', 'Prenom': 'Yann'},
 {'IdSportif': 41, 'Nom': 'DELOVINA', 'Prenom': 'Elina'},
 {'IdSportif': 42, 'Nom': 'LEHOUX', 'Prenom': 'Bruno'},
 {'IdSportif': 43, 'Nom': 'MATHIEU', 'Prenom': 'Denise'},
 {'IdSportif': 44, 'Nom': 'ADIBO', 'Prenom': 'Senamé'},
 {'IdSportif': 45, 'Nom': 'CHAVANT', 'Prenom': 'Christophe'},
 {'IdSportif': 46, 'Nom': 'DAUXIAN', 'Prenom': 'Cédric'},
 {'IdSportif': 47, 'Nom': 'GROEN', 'Prenom': 'Céline'},
 {'IdSportif': 48, 'Nom': 'HEDDI', 'Prenom': 'Zohra'},
 {'IdSportif': 49, 'Nom': 'JOUVE', 'Prenom': 'Sandra'},
 {'IdSportif': 50, 'Nom': 'KALOMBO', 'Prenom': 'Yannick'},
 {'IdSportif': 51, 'Nom': 'LOPEZ', 'Prenom': 'Thibaud'},
 {'IdSportif': 52, 'Nom': 'DANDOIS', 'Prenom': 'Régis'},
 {'IdSportif': 53, 'Nom': 'DEMMER', 'Prenom': 'Thomas'},
 {'IdSportif': 54, 'Nom': 'ELKABBADJ', 'Prenom': 'Mohammed'},
 {'IdSportif': 55, 'Nom': 'FEROLDI', 'Prenom': 'Olivier'},
 {'IdSportif': 56, 'Nom': 'GUERRAOUI', 'Prenom': 'Zohra'},
 {'IdSportif': 57, 'Nom': 'BOISSEAU', 'Prenom': 'Eric'},
 {'IdSportif': 58, 'Nom': 'GUIGUI', 'Prenom': 'Vincent'},
 {'IdSportif': 59, 'Nom': 'CLERICE', 'Prenom': 'Stéphanie'},
 {'IdSportif': 61, 'Nom': 'LAZARRE', 'Prenom': 'Jean'},
 {'IdSportif': 63, 'Nom': 'AMAND', 'Prenom': 'Patrick'},
 {'IdSportif': 64, 'Nom': 'LANOE', 'Prenom': 'Françoise'},
 {'IdSportif': 65, 'Nom': 'CHESNIER', 'Prenom': 'Marc'},
 {'IdSportif': 66, 'Nom': 'DURIEU', 'Prenom': 'Loïc'},
 {'IdSportif': 67, 'Nom': 'VONTHRON', 'Prenom': 'Dominique'},
 {'IdSportif': 68, 'Nom': 'REGNAULD', 'Prenom': 'Jeanne'},
 {'IdSportif': 69, 'Nom': 'MARIE', 'Prenom': 'Paule'},
 {'IdSportif': 70, 'Nom': 'BELLAMY', 'Prenom': 'Norbert'},
 {'IdSportif': 71, 'Nom': 'DELAROCHE', 'Prenom': 'Bertrand'},
 {'IdSportif': 72, 'Nom': 'MARTEL', 'Prenom': 'Bernard'},
 {'IdSportif': 73, 'Nom': 'DALLIER', 'Prenom': 'Didier'},
 {'IdSportif': 74, 'Nom': 'AUVRAY', 'Prenom': 'Alain'},
 {'IdSportif': 76, 'Nom': 'HALGATTE', 'Prenom': 'Claude'},
 {'IdSportif': 77, 'Nom': 'HEON', 'Prenom': 'Philippe'},
 {'IdSportif': 78, 'Nom': 'CHAUVIN', 'Prenom': 'Julien'},
 {'IdSportif': 79, 'Nom': 'HENRY', 'Prenom': 'Jacky'},
 {'IdSportif': 80, 'Nom': 'HOCHET', 'Prenom': 'Pierre'},
 {'IdSportif': 81, 'Nom': 'DROULLON', 'Prenom': 'Joëlle'},
 {'IdSportif': 83, 'Nom': 'LEROUX', 'Prenom': 'André'},
 {'IdSportif': 84, 'Nom': 'SALLAÏ', 'Prenom': 'Miloud'},
 {'IdSportif': 85, 'Nom': 'HAMARD', 'Prenom': 'Romain'},
 {'IdSportif': 86, 'Nom': 'GALLOT', 'Prenom': 'Bernard'},
 {'IdSportif': 87, 'Nom': 'COUESBOT', 'Prenom': 'Daniel'},
 {'IdSportif': 88, 'Nom': 'LEJEUNE', 'Prenom': 'Richard'},
 {'IdSportif': 89, 'Nom': 'RIQUIER', 'Prenom': 'Jean-pierre'},
 {'IdSportif': 90, 'Nom': 'DUREL', 'Prenom': 'Eric'},
 {'IdSportif': 91, 'Nom': 'LECHEVALIER', 'Prenom': 'Patrick'},
 {'IdSportif': 92, 'Nom': 'HERVIEU', 'Prenom': 'Jean-François'},
 {'IdSportif': 93, 'Nom': 'CAUCHARD', 'Prenom': 'Georges'},
 {'IdSportif': 95, 'Nom': 'EUSTACHE', 'Prenom': 'Marcel'},
 {'IdSportif': 96, 'Nom': 'JANY', 'Prenom': 'Claude'},
 {'IdSportif': 97, 'Nom': 'BONHOMME', 'Prenom': 'Bruno'},
 {'IdSportif': 98, 'Nom': 'RICHARD', 'Prenom': 'William'},
 {'IdSportif': 100, 'Nom': 'LESOIF', 'Prenom': 'Jacques'},
 {'IdSportif': 101, 'Nom': 'SWERTVAEGER', 'Prenom': 'Eric'},
 {'IdSportif': 102, 'Nom': 'DUVAL', 'Prenom': 'Alain'},
 {'IdSportif': 103, 'Nom': 'LEMENOREL', 'Prenom': 'Claude'},
 {'IdSportif': 104, 'Nom': 'SEHIER', 'Prenom': 'Dominique'},
 {'IdSportif': 105, 'Nom': 'STILO', 'Prenom': 'Philippe'},
 {'IdSportif': 106, 'Nom': 'LE BANNARD', 'Prenom': 'Gérard'},
 {'IdSportif': 107, 'Nom': 'BORNE', 'Prenom': 'Jean-Yves'},
 {'IdSportif': 108, 'Nom': 'LAURENCE', 'Prenom': 'Serge'},
 {'IdSportif': 109, 'Nom': 'SAUVAGE', 'Prenom': 'Patrick'},
 {'IdSportif': 110, 'Nom': 'ROULLAND', 'Prenom': 'Christian'},
 {'IdSportif': 111, 'Nom': 'LESIEUR', 'Prenom': 'Michel'},
 {'IdSportif': 112, 'Nom': 'LUYCKX', 'Prenom': 'Gérard'},
 {'IdSportif': 113, 'Nom': 'AVICE', 'Prenom': 'Benoît'},
 {'IdSportif': 114, 'Nom': 'MICHEL', 'Prenom': 'Danielle'},
 {'IdSportif': 115, 'Nom': 'LEMOUSSU', 'Prenom': 'Laurent'},
 {'IdSportif': 116, 'Nom': 'BELUAU', 'Prenom': 'Gilberte'},
 {'IdSportif': 117, 'Nom': 'FERREIRA', 'Prenom': 'Martine'},
 {'IdSportif': 118, 'Nom': 'FROMENT', 'Prenom': 'Philippe'},
 {'IdSportif': 119, 'Nom': 'GUITON', 'Prenom': 'Jean-paul'},
 {'IdSportif': 120, 'Nom': 'LECOMTE', 'Prenom': 'Christian'},
 {'IdSportif': 121, 'Nom': 'HUET', 'Prenom': 'Loïc'},
 {'IdSportif': 122, 'Nom': 'BECQUET', 'Prenom': 'Erika'},
 {'IdSportif': 123, 'Nom': 'LETHIMONNIER', 'Prenom': 'Victor'},
 {'IdSportif': 124, 'Nom': 'SWERTVAEGER', 'Prenom': 'Michelle'},
 {'IdSportif': 125, 'Nom': 'JALON', 'Prenom': 'Patrick'},
 {'IdSportif': 126, 'Nom': 'DEBOUT', 'Prenom': 'Eric'},
 {'IdSportif': 127, 'Nom': 'GASTELLIER', 'Prenom': 'Charles'},
 {'IdSportif': 128, 'Nom': 'GIRONIE', 'Prenom': 'André'},
 {'IdSportif': 129, 'Nom': 'DABON', 'Prenom': 'Richard'},
 {'IdSportif': 130, 'Nom': 'LECHAUVE', 'Prenom': 'Jean-Claude'},
 {'IdSportif': 131, 'Nom': 'GALLOIS', 'Prenom': 'Michelle'},
 {'IdSportif': 132, 'Nom': 'DORON', 'Prenom': 'Yannick'},
 {'IdSportif': 133, 'Nom': 'LENEVEU', 'Prenom': 'Julien'},
 {'IdSportif': 134, 'Nom': 'LERICHE', 'Prenom': 'Harry'},
 {'IdSportif': 135, 'Nom': 'MANCEL', 'Prenom': 'Jean-luc'},
 {'IdSportif': 136, 'Nom': 'LABOULAIS', 'Prenom': 'Chloé'},
 {'IdSportif': 137, 'Nom': 'DUDOUIT', 'Prenom': 'Chloé'},
 {'IdSportif': 138, 'Nom': 'MADELAINE', 'Prenom': 'Chloé'},
 {'IdSportif': 139, 'Nom': 'BESNARD', 'Prenom': 'Chloé'},
 {'IdSportif': 140, 'Nom': 'BELZ', 'Prenom': 'Chloé'},
 {'IdSportif': 141, 'Nom': 'BONNET', 'Prenom': 'Chloé'},
 {'IdSportif': 142, 'Nom': 'CORNET', 'Prenom': 'Chloé'},
 {'IdSportif': 143, 'Nom': 'BEUZELIN', 'Prenom': 'Chloé'},
 {'IdSportif': 144, 'Nom': 'GRANDIDIER', 'Prenom': 'Chloé'},
 {'IdSportif': 145, 'Nom': 'LENEVEU', 'Prenom': 'Marie'},
 {'IdSportif': 146, 'Nom': 'DABON', 'Prenom': 'Rick'},
 {'IdSportif': 147, 'Nom': 'CLERICE', 'Prenom': 'Alice'},
 {'IdSportif': 148, 'Nom': 'COMES', 'Prenom': 'Marie'},
 {'IdSportif': 149, 'Nom': 'BELZ', 'Prenom': 'Sylvianne'},
 {'IdSportif': 150, 'Nom': 'BELZ', 'Prenom': 'Paul'},
 {'IdSportif': 151, 'Nom': 'HENRY', 'Prenom': 'Maël'}]
2. Quels sont les gymnases de "Villetaneuse" ou de "Sarcelles" qui ont une surface de plus de 400 m2 ?
In [3]:
list(db.Gymnases.find(
    {
        "Ville": { "$in": [ "VILLETANEUSE", "SARCELLES"]},
        "Surface": { "$gt": 400 }
    },
    {
        "_id": 0,
        "NomGymnase": 1,
        "Ville": 1,
        "Surface": 1
    }))
Out[3]:
[{'NomGymnase': 'PAUL ELUARD', 'Surface': 500, 'Ville': 'SARCELLES'},
 {'NomGymnase': 'BRASSENS', 'Surface': 620, 'Ville': 'SARCELLES'},
 {'NomGymnase': 'CAMUS', 'Surface': 620, 'Ville': 'SARCELLES'},
 {'NomGymnase': 'SAMOURAI', 'Surface': 600, 'Ville': 'SARCELLES'},
 {'NomGymnase': 'DOLTO', 'Surface': 620, 'Ville': 'VILLETANEUSE'},
 {'NomGymnase': 'MERMOZ', 'Surface': 600, 'Ville': 'VILLETANEUSE'}]
3. Quels sont les sportifs (identifiant et nom) qui pratiquent du hand ball ?
In [4]:
list(db.Sportifs.find(
    {
        "Sports.Jouer": "Hand ball"
    },
    {
        "_id": 0,
        "IdSportif": 1,
        "Nom": 1
    }
))
Out[4]:
[{'IdSportif': 6, 'Nom': 'RETALDI'},
 {'IdSportif': 19, 'Nom': 'TESTEMONT'},
 {'IdSportif': 20, 'Nom': 'TIENER'},
 {'IdSportif': 25, 'Nom': 'MARTORA'},
 {'IdSportif': 26, 'Nom': 'MICHEL'},
 {'IdSportif': 27, 'Nom': 'NIELLEZ'},
 {'IdSportif': 28, 'Nom': 'RABAHI'},
 {'IdSportif': 29, 'Nom': 'ROUSSEL'},
 {'IdSportif': 30, 'Nom': 'SCHINK'},
 {'IdSportif': 31, 'Nom': 'STEMPUT'},
 {'IdSportif': 32, 'Nom': 'VAN CAUTER'},
 {'IdSportif': 33, 'Nom': 'RAMPNOUX'},
 {'IdSportif': 34, 'Nom': 'LHERPINIERE'},
 {'IdSportif': 35, 'Nom': 'TANQUE'},
 {'IdSportif': 38, 'Nom': 'VASSEMON'},
 {'IdSportif': 39, 'Nom': 'MOREL'},
 {'IdSportif': 40, 'Nom': 'VALIN'},
 {'IdSportif': 43, 'Nom': 'MATHIEU'},
 {'IdSportif': 51, 'Nom': 'LOPEZ'},
 {'IdSportif': 60, 'Nom': 'TIZEGHAT'},
 {'IdSportif': 67, 'Nom': 'VONTHRON'},
 {'IdSportif': 68, 'Nom': 'REGNAULD'},
 {'IdSportif': 69, 'Nom': 'MARIE'},
 {'IdSportif': 72, 'Nom': 'MARTEL'},
 {'IdSportif': 75, 'Nom': 'SARRAZIN'},
 {'IdSportif': 83, 'Nom': 'LEROUX'},
 {'IdSportif': 84, 'Nom': 'SALLAÏ'},
 {'IdSportif': 89, 'Nom': 'RIQUIER'},
 {'IdSportif': 94, 'Nom': 'LEROUX'},
 {'IdSportif': 98, 'Nom': 'RICHARD'},
 {'IdSportif': 100, 'Nom': 'LESOIF'},
 {'IdSportif': 101, 'Nom': 'SWERTVAEGER'},
 {'IdSportif': 104, 'Nom': 'SEHIER'},
 {'IdSportif': 105, 'Nom': 'STILO'},
 {'IdSportif': 109, 'Nom': 'SAUVAGE'},
 {'IdSportif': 110, 'Nom': 'ROULLAND'},
 {'IdSportif': 111, 'Nom': 'LESIEUR'},
 {'IdSportif': 112, 'Nom': 'LUYCKX'},
 {'IdSportif': 114, 'Nom': 'MICHEL'},
 {'IdSportif': 123, 'Nom': 'LETHIMONNIER'},
 {'IdSportif': 124, 'Nom': 'SWERTVAEGER'},
 {'IdSportif': 135, 'Nom': 'MANCEL'},
 {'IdSportif': 138, 'Nom': 'MADELAINE'},
 {'IdSportif': 151, 'Nom': 'HENRY'}]
4. Dans quels gymnases et quels jours y a t-il des séances de hand ball ?
In [5]:
list(db.Gymnases.find(
    {
        "Seances.Libelle": "Hand ball"
    },
    {
        "_id": 0,
        "NomGymnase": 1,
        "Ville": 1,
        "Seances.Jour": 1,
        "Seances.Libelle": 1
    }
))
Out[5]:
[{'NomGymnase': 'PAUL ELUARD',
  'Seances': [{'Jour': 'Samedi', 'Libelle': 'Basket ball'},
   {'Jour': 'Lundi', 'Libelle': 'Hand ball'},
   {'Jour': 'Lundi', 'Libelle': 'Hand ball'},
   {'Jour': 'Lundi', 'Libelle': 'Hand ball'},
   {'Jour': 'Lundi', 'Libelle': 'Hand ball'},
   {'Jour': 'lundi', 'Libelle': 'Hand ball'},
   {'Jour': 'Lundi', 'Libelle': 'Hand ball'},
   {'Jour': 'Dimanche', 'Libelle': 'Hand ball'},
   {'Jour': 'Dimanche', 'Libelle': 'Hand ball'},
   {'Jour': 'mardi', 'Libelle': 'Hand ball'},
   {'Jour': 'mercredi', 'Libelle': 'Hand ball'},
   {'Jour': 'Samedi', 'Libelle': 'Hand ball'},
   {'Jour': 'Samedi', 'Libelle': 'Hand ball'},
   {'Jour': 'Samedi', 'Libelle': 'Hand ball'},
   {'Jour': 'jeudi', 'Libelle': 'Hand ball'},
   {'Jour': 'lundi', 'Libelle': 'Hand ball'},
   {'Jour': 'lundi', 'Libelle': 'Hand ball'},
   {'Jour': 'lundi', 'Libelle': 'Hand ball'},
   {'Jour': 'lundi', 'Libelle': 'Hand ball'},
   {'Jour': 'mercredi', 'Libelle': 'Hockey'}],
  'Ville': 'STAINS'},
 {'NomGymnase': 'GARCIA LORCA',
  'Seances': [{'Jour': 'samedi', 'Libelle': 'Volley ball'},
   {'Jour': 'vendredi', 'Libelle': 'Volley ball'},
   {'Jour': 'dimanche', 'Libelle': 'Volley ball'},
   {'Jour': 'dimanche', 'Libelle': 'Hand ball'},
   {'Jour': 'mardi', 'Libelle': 'Hand ball'},
   {'Jour': 'mardi', 'Libelle': 'Hockey'}],
  'Ville': 'STAINS'}]
In [6]:
# **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
    }}
]))
Out[6]:
[{'_id': {'Jour': 'lundi', 'Ville': 'STAINS'}, 'nb': 10},
 {'_id': {'Jour': 'samedi', 'Ville': 'STAINS'}, 'nb': 3},
 {'_id': {'Jour': 'dimanche', 'Ville': 'STAINS'}, 'nb': 3},
 {'_id': {'Jour': 'mardi', 'Ville': 'STAINS'}, 'nb': 2},
 {'_id': {'Jour': 'jeudi', 'Ville': 'STAINS'}, 'nb': 1},
 {'_id': {'Jour': 'mercredi', 'Ville': 'STAINS'}, 'nb': 1}]
5. Dans quels gymnases peut-on jouer au hockey le mercredi apres 15H ?
In [7]:
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
    }
))
Out[7]:
[{'NomGymnase': 'PAUL ELUARD',
  'Seances': [{'Horaire': 9.0, 'Jour': 'Samedi', 'Libelle': 'Basket ball'},
   {'Horaire': 9.0, 'Jour': 'Lundi', 'Libelle': 'Hand ball'},
   {'Horaire': 10.0, 'Jour': 'Lundi', 'Libelle': 'Hand ball'},
   {'Horaire': 11.3, 'Jour': 'Lundi', 'Libelle': 'Hand ball'},
   {'Horaire': 14.0, 'Jour': 'Lundi', 'Libelle': 'Hand ball'},
   {'Horaire': 17.3, 'Jour': 'lundi', 'Libelle': 'Hand ball'},
   {'Horaire': 19.3, 'Jour': 'Lundi', 'Libelle': 'Hand ball'},
   {'Horaire': 17.3, 'Jour': 'Dimanche', 'Libelle': 'Hand ball'},
   {'Horaire': 19.3, 'Jour': 'Dimanche', 'Libelle': 'Hand ball'},
   {'Horaire': 17.3, 'Jour': 'mardi', 'Libelle': 'Hand ball'},
   {'Horaire': 17.3, 'Jour': 'mercredi', 'Libelle': 'Hand ball'},
   {'Horaire': 15.3, 'Jour': 'Samedi', 'Libelle': 'Hand ball'},
   {'Horaire': 16.3, 'Jour': 'Samedi', 'Libelle': 'Hand ball'},
   {'Horaire': 17.3, 'Jour': 'Samedi', 'Libelle': 'Hand ball'},
   {'Horaire': 20.0, 'Jour': 'jeudi', 'Libelle': 'Hand ball'},
   {'Horaire': 14.0, 'Jour': 'lundi', 'Libelle': 'Hand ball'},
   {'Horaire': 18.0, 'Jour': 'lundi', 'Libelle': 'Hand ball'},
   {'Horaire': 19.0, 'Jour': 'lundi', 'Libelle': 'Hand ball'},
   {'Horaire': 20.0, 'Jour': 'lundi', 'Libelle': 'Hand ball'},
   {'Horaire': 17.0, 'Jour': 'mercredi', 'Libelle': 'Hockey'}],
  'Ville': 'STAINS'},
 {'NomGymnase': 'PAUL ELUARD',
  'Seances': [{'Horaire': 10.0, 'Jour': 'Vendredi', 'Libelle': 'Basket ball'},
   {'Horaire': 19.0, 'Jour': 'mercredi', 'Libelle': 'Hockey'}],
  'Ville': 'SARCELLES'},
 {'NomGymnase': 'RAVEL',
  'Seances': [{'Horaire': 19.0, 'Jour': 'dimanche', 'Libelle': 'Volley ball'},
   {'Horaire': 20.0, 'Jour': 'mercredi', 'Libelle': 'Hockey'},
   {'Horaire': 17.0, 'Jour': 'lundi', 'Libelle': 'Hockey'}],
  'Ville': 'STAINS'},
 {'NomGymnase': 'SAMOURAI',
  'Seances': [{'Horaire': 16.3, 'Jour': 'lundi', 'Libelle': 'Volley ball'},
   {'Horaire': 17.0, 'Jour': 'Lundi', 'Libelle': 'Volley ball'},
   {'Horaire': 18.0, 'Jour': 'Lundi', 'Libelle': 'Volley ball'},
   {'Horaire': 19.0, 'Jour': 'lundi', 'Libelle': 'Volley ball'},
   {'Horaire': 20.0, 'Jour': 'Lundi', 'Libelle': 'Volley ball'},
   {'Horaire': 19.0, 'Jour': 'mercredi', 'Libelle': 'Hockey'}],
  'Ville': 'SARCELLES'},
 {'NomGymnase': 'PABLO NERUDA',
  'Seances': [{'Horaire': 16.3, 'Jour': 'lundi', 'Libelle': 'Volley ball'},
   {'Horaire': 19.0, 'Jour': 'mardi', 'Libelle': 'Volley ball'},
   {'Horaire': 14.0, 'Jour': 'mercredi', 'Libelle': 'Hockey'},
   {'Horaire': 16.0, 'Jour': 'mercredi', 'Libelle': 'Hockey'}],
  'Ville': 'PIERREFITTE'},
 {'NomGymnase': 'LUMIERES',
  'Seances': [{'Horaire': 19.0, 'Jour': 'mercredi', 'Libelle': 'Hockey'}],
  'Ville': 'SARCELLES'},
 {'NomGymnase': 'SIMON',
  'Seances': [{'Horaire': 16.3, 'Jour': 'lundi', 'Libelle': 'Volley ball'},
   {'Horaire': 19.0, 'Jour': 'mardi', 'Libelle': 'Volley ball'},
   {'Horaire': 17.0, 'Jour': 'mercredi', 'Libelle': 'Hockey'}],
  'Ville': 'STAINS'},
 {'NomGymnase': 'PASCAL',
  'Seances': [{'Horaire': 10.0, 'Jour': 'jeudi', 'Libelle': 'Volley ball'},
   {'Horaire': 14.0, 'Jour': 'mercredi', 'Libelle': 'Hockey'},
   {'Horaire': 17.0, 'Jour': 'mercredi', 'Libelle': 'Hockey'}],
  'Ville': 'VILLETANEUSE'}]
In [8]:
# **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
    }}
]))
Out[8]:
[{'Gymnase': 'LUMIERES', 'Ville': 'SARCELLES'},
 {'Gymnase': 'PABLO NERUDA', 'Ville': 'PIERREFITTE'},
 {'Gymnase': 'PASCAL', 'Ville': 'VILLETANEUSE'},
 {'Gymnase': 'PAUL ELUARD', 'Ville': 'SARCELLES'},
 {'Gymnase': 'PAUL ELUARD', 'Ville': 'STAINS'},
 {'Gymnase': 'RAVEL', 'Ville': 'STAINS'},
 {'Gymnase': 'SAMOURAI', 'Ville': 'SARCELLES'},
 {'Gymnase': 'SIMON', 'Ville': 'STAINS'}]
6. Quels sportifs (identifiant et nom) ne pratiquent aucun sport ?
In [9]:
list(db.Sportifs.find(
    {
        "Sports" : { "$exists" : False }
    },
    {
        "_id": 0,
        "Nom":  1
    }
))
Out[9]:
[{'Nom': 'DROULLON'},
 {'Nom': 'BELUAU'},
 {'Nom': 'FERREIRA'},
 {'Nom': 'GALLOIS'},
 {'Nom': 'LENEVEU'},
 {'Nom': 'DABON'},
 {'Nom': 'CLERICE'},
 {'Nom': 'COMES'},
 {'Nom': 'BELZ'}]
7. Quels gymnases n'ont pas de séances le dimanche ?
In [10]:
list(db.Gymnases.find(
    {
        "Seances.Jour" : { "$nin" : [ "dimanche", "Dimanche" ]}
    },
    {
        "_id": 0,
        "NomGymnase": 1,
        "Ville": 1,
        "Seances.Jour": 1
    }
))
Out[10]:
[{'NomGymnase': 'SAINT EXUPERY',
  'Seances': [{'Jour': 'Mercredi'}, {'Jour': 'lundi'}, {'Jour': 'jeudi'}],
  'Ville': 'PIERREFITTE'},
 {'NomGymnase': 'PAUL ELUARD',
  'Seances': [{'Jour': 'Vendredi'}, {'Jour': 'mercredi'}],
  'Ville': 'SARCELLES'},
 {'NomGymnase': 'BRASSENS',
  'Seances': [{'Jour': 'lundi'}, {'Jour': 'jeudi'}],
  'Ville': 'SARCELLES'},
 {'NomGymnase': 'VERLAINE',
  'Seances': [{'Jour': 'vendredi'}, {'Jour': 'jeudi'}],
  'Ville': 'STAINS'},
 {'NomGymnase': 'JULES FERRY', 'Ville': 'PIERREFITTE'},
 {'NomGymnase': 'CAMUS',
  'Seances': [{'Jour': 'samedi'}],
  'Ville': 'SARCELLES'},
 {'NomGymnase': 'LAMARTINE', 'Ville': 'PIERREFITTE'},
 {'NomGymnase': 'BREL',
  'Seances': [{'Jour': 'lundi'}],
  'Ville': 'PIERREFITTE'},
 {'NomGymnase': 'SAMOURAI',
  'Seances': [{'Jour': 'lundi'},
   {'Jour': 'Lundi'},
   {'Jour': 'Lundi'},
   {'Jour': 'lundi'},
   {'Jour': 'Lundi'},
   {'Jour': 'mercredi'}],
  'Ville': 'SARCELLES'},
 {'NomGymnase': 'PABLO NERUDA',
  'Seances': [{'Jour': 'lundi'},
   {'Jour': 'mardi'},
   {'Jour': 'mercredi'},
   {'Jour': 'mercredi'}],
  'Ville': 'PIERREFITTE'},
 {'NomGymnase': 'LUMIERES',
  'Seances': [{'Jour': 'mercredi'}],
  'Ville': 'SARCELLES'},
 {'NomGymnase': 'SIMON',
  'Seances': [{'Jour': 'lundi'}, {'Jour': 'mardi'}, {'Jour': 'mercredi'}],
  'Ville': 'STAINS'},
 {'NomGymnase': 'BARBARA',
  'Seances': [{'Jour': 'mardi'}],
  'Ville': 'SAINT DENIS'},
 {'NomGymnase': 'ARAGON', 'Ville': 'SAINT DENIS'},
 {'NomGymnase': 'BELFEGOR',
  'Seances': [{'Jour': 'Jeudi'}, {'Jour': 'mercredi'}],
  'Ville': 'SAINT DENIS'},
 {'NomGymnase': 'MERMOZ', 'Ville': 'VILLETANEUSE'},
 {'NomGymnase': 'PASCAL',
  'Seances': [{'Jour': 'jeudi'}, {'Jour': 'mercredi'}, {'Jour': 'mercredi'}],
  'Ville': 'VILLETANEUSE'}]
8. Quels gymnases ne proposent que des séances de basket ball ou de volley ball ?
In [11]:
list(db.Gymnases.find(
    {
        "$nor": [
            { "Seances.Libelle": { "$ne": "Basket ball" }},
            { "Seances.Libelle": { "$ne": "Volley ball" }}
        ]
    },
    {
        "_id": 0,
        "NomGymnase": 1,
        "Ville": 1,
        "Seances.Libelle": 1
    }
))
Out[11]:
[{'NomGymnase': 'SAINT EXUPERY',
  'Seances': [{'Libelle': 'Basket ball'},
   {'Libelle': 'Volley ball'},
   {'Libelle': 'Volley ball'}],
  'Ville': 'PIERREFITTE'},
 {'NomGymnase': 'CHOPIN',
  'Seances': [{'Libelle': 'Basket ball'}, {'Libelle': 'Volley ball'}],
  'Ville': 'MONTMORENCY'},
 {'NomGymnase': 'BELFEGOR',
  'Seances': [{'Libelle': 'Basket ball'}, {'Libelle': 'Volley ball'}],
  'Ville': 'SAINT DENIS'}]
9. Quels sont les entraîneurs qui sont aussi joueurs ?
In [12]:
list(db.Sportifs.find(
    {
        "Sports.Jouer" : { "$exists" : True },
        "Sports.Entrainer" : { "$exists" : True }
    },
    {
        "_id": 0,
        "Nom":  1
    }
))
Out[12]:
[{'Nom': 'BOUTAHAR'},
 {'Nom': 'KERVADEC'},
 {'Nom': 'HUE'},
 {'Nom': 'DORLEANS'},
 {'Nom': 'RETALDI'},
 {'Nom': 'GOMEZ'},
 {'Nom': 'ROUSSEL'},
 {'Nom': 'SCHINK'},
 {'Nom': 'STEMPUT'},
 {'Nom': 'VAN CAUTER'},
 {'Nom': 'TANQUE'},
 {'Nom': 'DJELOUDANE'},
 {'Nom': 'VASSEMON'},
 {'Nom': 'VALIN'},
 {'Nom': 'HEDDI'},
 {'Nom': 'KALOMBO'},
 {'Nom': 'GUERRAOUI'},
 {'Nom': 'BOISSEAU'},
 {'Nom': 'GUIGUI'},
 {'Nom': 'CLERICE'},
 {'Nom': 'TIZEGHAT'},
 {'Nom': 'LAZARRE'},
 {'Nom': 'BELZ'},
 {'Nom': 'HENRY'}]
10. Quels sont les sportifs qui sont des conseillers ?
In [13]:
list(db.Sportifs.find(
    {
        "IdSportif": { "$in": db.Sportifs.distinct("IdSportifConseiller")}
    },
    {
        "_id": 0,
        "Nom":  1
    }
))
Out[13]:
[{'Nom': 'BOUTAHAR'},
 {'Nom': 'KERVADEC'},
 {'Nom': 'HUE'},
 {'Nom': 'DORLEANS'},
 {'Nom': 'COMES'},
 {'Nom': 'RETALDI'},
 {'Nom': 'GOMEZ'},
 {'Nom': 'ANTUNES'},
 {'Nom': 'CAILLIOT'},
 {'Nom': 'LE BOUCHER'},
 {'Nom': 'ADIBO'},
 {'Nom': 'RICHARD'}]
11. Pour le sportif "Kervadec" quel est le nom de son conseiller ?
In [14]:
list(db.Sportifs.find(
    {
        "IdSportif": list(db.Sportifs.find({ "Nom": "KERVADEC" }))[0]["IdSportifConseiller"]
    },
    {
        "_id": 0,
        "Sports": 0
    }
))
Out[14]:
[{'Age': 30,
  'IdSportif': 1,
  'Nom': 'BOUTAHAR',
  'Prenom': 'Abderahim',
  'Sexe': 'm'}]
12. Quels entraîneurs entraînent du hand ball et du basket ball ?
In [15]:
list(db.Sportifs.find(
    {
        "Sports.Entrainer": "Hand ball", 
        "Sports.Entrainer": "Basket ball" 
    },
    {
        "_id": 0,
        "Nom": 1,
        "Sports.Entrainer": 1
    }
))
Out[15]:
[{'Nom': 'BOUTAHAR',
  'Sports': {'Entrainer': ['Basket ball',
    'Volley ball',
    'Hand ball',
    'Hockey',
    'Badmington']}},
 {'Nom': 'KERVADEC',
  'Sports': {'Entrainer': ['Basket ball',
    'Volley ball',
    'Hand ball',
    'Tennis',
    'Hockey',
    'Badmington',
    'Ping pong',
    'Boxe']}},
 {'Nom': 'HUE',
  'Sports': {'Entrainer': ['Basket ball',
    'Volley ball',
    'Hand ball',
    'Badmington']}},
 {'Nom': 'DORLEANS',
  'Sports': {'Entrainer': ['Basket ball', 'Ping pong', 'Boxe']}},
 {'Nom': 'BELZ', 'Sports': {'Entrainer': 'Basket ball'}},
 {'Nom': 'HENRY', 'Sports': {'Entrainer': ['Basket ball', 'Hand ball']}}]
In [16]:
list(db.Sportifs.find(
    {
        "$and": [
            { "Sports.Entrainer": "Hand ball" }, 
            { "Sports.Entrainer": "Basket ball" }
        ]        
    },
    {
        "_id": 0,
        "Nom": 1,
        "Sports.Entrainer": 1
    }
))
Out[16]:
[{'Nom': 'BOUTAHAR',
  'Sports': {'Entrainer': ['Basket ball',
    'Volley ball',
    'Hand ball',
    'Hockey',
    'Badmington']}},
 {'Nom': 'KERVADEC',
  'Sports': {'Entrainer': ['Basket ball',
    'Volley ball',
    'Hand ball',
    'Tennis',
    'Hockey',
    'Badmington',
    'Ping pong',
    'Boxe']}},
 {'Nom': 'HUE',
  'Sports': {'Entrainer': ['Basket ball',
    'Volley ball',
    'Hand ball',
    'Badmington']}},
 {'Nom': 'HENRY', 'Sports': {'Entrainer': ['Basket ball', 'Hand ball']}}]
13. Quels sont les couples de sportifs (identifiant et nom et prénom de chaque) de même age ?

NE PAS FAIRE

14. Quelle est la moyenne d'âge des sportives qui pratiquent du basket ball ?
In [17]:
list(db.Sportifs.aggregate([
    { "$match": { "Sports.Jouer": "Basket ball", "Sexe": { "$in": [ "f", "F" ]} }},
    { "$group": { "_id": None, "AgeMoyen": { "$avg": "$Age" }}}
]))
Out[17]:
[{'AgeMoyen': 24.6, '_id': None}]
15. Quels sont les sportifs les plus jeunes ?
In [18]:
# 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
Out[18]:
{'_id': None, 'agemin': 22}
In [19]:
# On cherche les sportifs avec `Age = agemin.agemin`.
list(db.Sportifs.find(
    {
        "Age": agemin["agemin"]
    },
    {
        "_id": 0,
        "Nom": 1,
        "Age": 1
    }
))
Out[19]:
[{'Age': 22, 'Nom': 'COMES'},
 {'Age': 22, 'Nom': 'BOYON'},
 {'Age': 22, 'Nom': 'COLOMB'},
 {'Age': 22, 'Nom': 'LE BOUCHER'},
 {'Age': 22, 'Nom': 'ROUSSEL'},
 {'Age': 22, 'Nom': 'STEMPUT'},
 {'Age': 22, 'Nom': 'DELOVINA'},
 {'Age': 22, 'Nom': 'KALOMBO'},
 {'Age': 22, 'Nom': 'DANDOIS'},
 {'Age': 22, 'Nom': 'DEMMER'},
 {'Age': 22, 'Nom': 'MARTEL'},
 {'Age': 22, 'Nom': 'HERVIEU'},
 {'Age': 22, 'Nom': 'MICHEL'}]
16. Quels sont les gymnases de "Stains" ou de "Montmorency" qui ont la plus grande surface ?
In [20]:
# On peut chercher les plus grands gymnases de chaque ville. 
grand = list(db.Gymnases.aggregate([
    { "$group": { "_id": "$Ville", "surfmax": { "$max": "$Surface" }} }
]))
grand
Out[20]:
[{'_id': 'SAINT DENIS', 'surfmax': 520},
 {'_id': 'STAINS', 'surfmax': 400},
 {'_id': 'MONTMORENCY', 'surfmax': 500},
 {'_id': 'GARGES', 'surfmax': 400},
 {'_id': 'VILLETANEUSE', 'surfmax': 620},
 {'_id': 'PIERREFITTE', 'surfmax': 450},
 {'_id': 'SARCELLES', 'surfmax': 620}]
In [21]:
# Ensuite, on cherche comme ci-dessus
17. Quels entraîneurs n'entraînent que du hand ball ou du basket ball ?
In [22]:
# On récupère la liste des sports entraînés.
sports = db.Sportifs.distinct("Sports.Entrainer")
sports
Out[22]:
['Badmington',
 'Basket ball',
 'Hand ball',
 'Hockey',
 'Volley ball',
 'Boxe',
 'Ping pong',
 'Tennis']
In [23]:
# On fait un filtre pour supprimer le hand et le basket.
autres = list(filter(lambda s: (s != "Hand ball") & (s != "Basket ball"), sports))
autres
Out[23]:
['Badmington', 'Hockey', 'Volley ball', 'Boxe', 'Ping pong', 'Tennis']
In [24]:
# 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
    }
))
Out[24]:
[{'Nom': 'BELZ', 'Sports': {'Entrainer': 'Basket ball'}},
 {'Nom': 'HENRY', 'Sports': {'Entrainer': ['Basket ball', 'Hand ball']}}]
18. Quels sont les couples de sportifs (identifiant et nom et prénom de chaque) de même âge avec le même conseiller ?

NE PAS FAIRE

19. Quels sportifs n'ont pas de conseillers ?
In [25]:
list(db.Sportifs.find(
    {
        "IdSportifConseiller": { "$exists": False }
    },
    {
        "_id": 0,
        "Nom": 1
    }
))
Out[25]:
[{'Nom': 'BOUTAHAR'},
 {'Nom': 'CHAVANT'},
 {'Nom': 'CLERICE'},
 {'Nom': 'CHESNIER'},
 {'Nom': 'CHAUVIN'},
 {'Nom': 'RIQUIER'},
 {'Nom': 'BONHOMME'},
 {'Nom': 'BONE'}]
20. Pour chaque sportif donner le nombre de sports qu'il arbitre
In [26]:
list(db.Sportifs.aggregate([
    { "$match": { "Sports.Arbitrer": { "$exists": True }}},
    { "$unwind": "$Sports.Arbitrer"},
    { "$group": { "_id": "$Nom", "nbArbitrer": { "$sum": 1 }}}
]))
Out[26]:
[{'_id': 'STILO', 'nbArbitrer': 1},
 {'_id': 'RICHARD', 'nbArbitrer': 1},
 {'_id': 'LEROUX', 'nbArbitrer': 1},
 {'_id': 'TIZEGHAT', 'nbArbitrer': 1},
 {'_id': 'CLERICE', 'nbArbitrer': 1},
 {'_id': 'TANQUE', 'nbArbitrer': 1},
 {'_id': 'DORLEANS', 'nbArbitrer': 4},
 {'_id': 'ROUSSEL', 'nbArbitrer': 1},
 {'_id': 'BELZ', 'nbArbitrer': 1},
 {'_id': 'COMES', 'nbArbitrer': 1},
 {'_id': 'HUE', 'nbArbitrer': 3},
 {'_id': 'RETALDI', 'nbArbitrer': 3},
 {'_id': 'KERVADEC', 'nbArbitrer': 2},
 {'_id': 'GOMEZ', 'nbArbitrer': 3},
 {'_id': 'VAN CAUTER', 'nbArbitrer': 1},
 {'_id': 'BOUTAHAR', 'nbArbitrer': 3},
 {'_id': 'TESTEMONT', 'nbArbitrer': 1},
 {'_id': 'HENRY', 'nbArbitrer': 2},
 {'_id': 'TIENER', 'nbArbitrer': 1}]
21. Pour chaque gymnase de Stains donner par jour d'ouverture les horaires des premières et dernières séances
In [27]:
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" }}}
]))
Out[27]:
[{'_id': {'jour': 'mardi', 'nom': 'SIMON'}, 'debut': 19.0, 'fin': 19.0},
 {'_id': {'jour': 'dimanche', 'nom': 'GARCIA LORCA'},
  'debut': 17.0,
  'fin': 18.0},
 {'_id': {'jour': 'mardi', 'nom': 'GARCIA LORCA'}, 'debut': 17.0, 'fin': 20.0},
 {'_id': {'jour': 'vendredi', 'nom': 'GARCIA LORCA'},
  'debut': 17.3,
  'fin': 17.3},
 {'_id': {'jour': 'lundi', 'nom': 'RAVEL'}, 'debut': 17.0, 'fin': 17.0},
 {'_id': {'jour': 'dimanche', 'nom': 'RAVEL'}, 'debut': 19.0, 'fin': 19.0},
 {'_id': {'jour': 'mardi', 'nom': 'PAUL ELUARD'}, 'debut': 17.3, 'fin': 17.3},
 {'_id': {'jour': 'dimanche', 'nom': 'PAUL ELUARD'},
  'debut': 17.3,
  'fin': 19.3},
 {'_id': {'jour': 'lundi', 'nom': 'PAUL ELUARD'}, 'debut': 9.0, 'fin': 20.0},
 {'_id': {'jour': 'mercredi', 'nom': 'SIMON'}, 'debut': 17.0, 'fin': 17.0},
 {'_id': {'jour': 'mercredi', 'nom': 'PAUL ELUARD'},
  'debut': 17.0,
  'fin': 17.3},
 {'_id': {'jour': 'samedi', 'nom': 'GARCIA LORCA'},
  'debut': 17.3,
  'fin': 17.3},
 {'_id': {'jour': 'samedi', 'nom': 'RIMBAUD'}, 'debut': 19.0, 'fin': 19.0},
 {'_id': {'jour': 'jeudi', 'nom': 'PAUL ELUARD'}, 'debut': 20.0, 'fin': 20.0},
 {'_id': {'jour': 'mercredi', 'nom': 'RAVEL'}, 'debut': 20.0, 'fin': 20.0},
 {'_id': {'jour': 'samedi', 'nom': 'PAUL ELUARD'}, 'debut': 9.0, 'fin': 17.3},
 {'_id': {'jour': 'vendredi', 'nom': 'VERLAINE'}, 'debut': 19.0, 'fin': 19.0},
 {'_id': {'jour': 'jeudi', 'nom': 'VERLAINE'}, 'debut': 17.0, 'fin': 17.0},
 {'_id': {'jour': 'lundi', 'nom': 'SIMON'}, 'debut': 16.3, 'fin': 16.3},
 {'_id': {'jour': 'dimanche', 'nom': 'RIMBAUD'}, 'debut': 17.0, 'fin': 19.0}]
22. Pour chaque entraîneurs de hand ball quel est le nombre de séances journalières qu'il assure ?
In [28]:
# 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]
Out[28]:
[1, 2, 3, 7, 151]
In [29]:
# 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}}}
]))
Out[29]:
[{'_id': {'entraineur': 7, 'jour': 'mardi'}, 'nbSeances': 2},
 {'_id': {'entraineur': 3, 'jour': 'mardi'}, 'nbSeances': 1},
 {'_id': {'entraineur': 3, 'jour': 'vendredi'}, 'nbSeances': 1},
 {'_id': {'entraineur': 3, 'jour': 'samedi'}, 'nbSeances': 1},
 {'_id': {'entraineur': 7, 'jour': 'lundi'}, 'nbSeances': 1},
 {'_id': {'entraineur': 7, 'jour': 'dimanche'}, 'nbSeances': 1},
 {'_id': {'entraineur': 2, 'jour': 'mardi'}, 'nbSeances': 1},
 {'_id': {'entraineur': 7, 'jour': 'jeudi'}, 'nbSeances': 1},
 {'_id': {'entraineur': 7, 'jour': 'vendredi'}, 'nbSeances': 2},
 {'_id': {'entraineur': 2, 'jour': 'dimanche'}, 'nbSeances': 2},
 {'_id': {'entraineur': 1, 'jour': 'lundi'}, 'nbSeances': 6},
 {'_id': {'entraineur': 3, 'jour': 'dimanche'}, 'nbSeances': 1},
 {'_id': {'entraineur': 2, 'jour': 'mercredi'}, 'nbSeances': 1},
 {'_id': {'entraineur': 2, 'jour': 'samedi'}, 'nbSeances': 3},
 {'_id': {'entraineur': 3, 'jour': 'jeudi'}, 'nbSeances': 1},
 {'_id': {'entraineur': 3, 'jour': 'lundi'}, 'nbSeances': 4},
 {'_id': {'entraineur': 7, 'jour': 'mercredi'}, 'nbSeances': 6},
 {'_id': {'entraineur': 7, 'jour': 'samedi'}, 'nbSeances': 2}]
23. Quels sont les gymnases ayant plus de 15 séances le mercredi ?
In [30]:
# 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 }}}
]))
Out[30]:
[]
24. Pour chaque gymnase de Montmorency : quel est le nombre de séances journalières de chaque sport propose ?

A faire pour vous entraîner...

25. Dans quels gymnases et quels jours y a t-il au moins 4 séances de volley ball dans la journée ?

A faire pour vous entraîner...