Query’s uitvoeren op een MongoDB-database

Introductie tot MongoDB in Python

Filip Schouwenaars

Machine Learning Researcher

Databases en collecties

  • Database
    • Gerelateerde collecties
    • film
  • Collectie
    • Documenten met flexibele schema’s
    • movies

visual

Introductie tot MongoDB in Python

Databases en collecties in Python

from pymongo import MongoClient 
client = MongoClient()

# Beschikbare databases weergeven client.list_database_names()
['admin', 'config', 'film', 'local']
# Collecties in film-db weergeven
client.film.list_collection_names()
['movies']
Introductie tot MongoDB in Python

Alle documenten ophalen

# Client instellen
from pymongo import MongoClient 
client = MongoClient()

# Alle documenten ophalen client.film.movies.find()
# Punten vermijden mov = client.film.movies
mov.find()
Introductie tot MongoDB in Python

Van cursor naar Python-lijst

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

# Alle documenten ophalen
mov.find()
<pymongo.synchronous.cursor.Cursor at 0x7f...760>
list(mov.find())
[{'_id': '68...ff', 'title': 'superbad', ...
  • Cursor = verwijzing naar queryresultaten
  • Haal resultaten stuk voor stuk op
  • Die controle is handig
Introductie tot MongoDB in Python

Kijk mee

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

# find(), cursor naar lijst
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
    },
    ...
]
Introductie tot MongoDB in Python

Queryfilters

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
    },
    ...
]
Introductie tot MongoDB in Python

Specifieke records met .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
}
  • Geeft het eerste document dat op de query matcht terug
  • Retourneert een document, geen Cursor-object
Introductie tot MongoDB in Python

Laten we oefenen!

Introductie tot MongoDB in Python

Preparing Video For Download...