Interroger une base de données MongoDB

Introduction à MongoDB en Python

Filip Schouwenaars

Machine Learning Researcher

Bases de données et collections

  • Base de données
    • Collections liées
    • film
  • Collection
    • Documents à schéma souple
    • movies

visuel

Introduction à MongoDB en Python

Bases de données et collections en Python

from pymongo import MongoClient 
client = MongoClient()

# Lister les bases disponibles client.list_database_names()
['admin', 'config', 'film', 'local']
# Lister les collections de la BD film
client.film.list_collection_names()
['movies']
Introduction à MongoDB en Python

Récupérer tous les documents

# Configurer le client
from pymongo import MongoClient 
client = MongoClient()

# Retourner tous les documents client.film.movies.find()
# Éviter les points mov = client.film.movies
mov.find()
Introduction à MongoDB en Python

Du curseur à la liste Python

# Configurer le client
from pymongo import MongoClient 
client = MongoClient()
mov = client.film.movies

# Récupérer tous les documents
mov.find()
<pymongo.synchronous.cursor.Cursor at 0x7f...760>
list(mov.find())
[{'_id': '68...ff', 'title': 'superbad', ...
  • Curseur = pointeur vers les résultats
  • Permet d'extraire un à un
  • Ce contrôle est utile
Introduction à MongoDB en Python

Regardons le résultat

# Configurer le client
from pymongo import MongoClient 
client = MongoClient()
mov = client.film.movies

# find(), curseur vers liste
list(mov.find())
[
    {
        "_id": "6824bb...e53adbf274ff",
        "title": "superbad",
        "genre": ["comedy", "teen"],
        "release_year": 2007,
        "rating": 7.6
    },
    {
        "_id": "6824bb...e53adbf27500",
        "title": "interstellar",
        "genre": ["adventure", "drama", "sci-fi"],
        "release_year": 2014,
        "rating": 8.6,
        "won_oscar": True
    },
    ...
]
Introduction à MongoDB en Python

Filtres de requête

from pymongo import MongoClient 
client = MongoClient()
mov = client.film.movies

curs = mov.find({ "won_oscar": True }) list(curs)
[
    {
        '_id': '6824bbd05644e53adbf27500',
        'genre': ['adventure', 'drama', 'sci-fi'],
        'rating': 8.6,
        'release_year': 2014,
        'title': 'interstellar',
        'won_oscar': True
    },
     {
        '_id': '6824bbd05644e53adbf27501',
        'genre': ['action', 'sci-fi', 'thriller'],
        'rating': 8.8,
        'release_year': 2010,
        'title': 'inception',
        'won_oscar': True
    },
    ...
]
Introduction à MongoDB en Python

Cibler des enregistrements avec .find_one()

from pymongo import MongoClient 

client = MongoClient()
mov = client.film.movies

mov.find_one({ "title": "parasite" })
{
    '_id': '6824bbd05644e53adbf27518',
    'genre': ['drama', 'thriller'],
    'rating': 8.5,
    'release_year': 2019,
    'title': 'parasite',
    'won_oscar': True
}
  • Renvoie le premier document correspondant
  • Retourne un document, pas un objet Cursor
Introduction à MongoDB en Python

Passons à la pratique !

Introduction à MongoDB en Python

Preparing Video For Download...