Query Operators
INTRODUCTION TO MONGODB IN PYTHON
Filip Schouwenaars
Machine Learning Researcher
The $in operator
from pymongo import MongoClient [
client = MongoClient() ...
mov = [Link] {
'_id': '6824bbd05644e53adbf2750f',
'title': 'iron man',
curs = [Link]({
'genre': ['action', 'adventure', 'sci-fi'],
"release_year": {
'rating': 7.9,
"$in": [2008, 2009]
'release_year': 2008,
}
},
})
{
'_id': '6824bbd05644e53adbf27525',
list(curs)
'title': 'dragonball evolution',
'genre': ['action', 'adventure', 'fantasy'],
'rating': 2.6,
'release_year': 2009
}
]
INTRODUCTION TO MONGODB IN PYTHON
The $in operator on arrays
mov.find_one({ mov.find_one({
"genre": "adventure" "genre": {
}) "$in": ["thriller", "fantasy"]
}
{ })
'_id': '6824bbd05644e53adbf27500',
'title': 'interstellar', {
'genre': ['adventure', 'drama', 'sci-fi'], '_id': '6824bbd05644e53adbf27501',
'rating': 8.6, 'title': 'inception',
'release_year': 2014, 'genre': ['action', 'sci-fi', 'thriller'],
'won_oscar': True 'rating': 8.8,
} 'release_year': 2010,
'won_oscar': True
}
INTRODUCTION TO MONGODB IN PYTHON
The $exists operator
curs = [Link]({ [
"won_oscar": { ...
"$exists": True {
} '_id': '6824bbd05644e53adbf27515',
}) 'title': '12 angry men',
'genre': ['crime', 'drama'],
'rating': 9,
list(curs)
'release_year': 1957,
'won_oscar': False
},
{
'_id': '6824bbd05644e53adbf27516',
'title': 'get out',
'genre': ['horror', 'mystery', 'thriller'],
'rating': 7.8,
'release_year': 2017,
'won_oscar': True
},
...
]
INTRODUCTION TO MONGODB IN PYTHON
Combining queries with $and
mov.find_one({ {
"$and": [ '_id': '6824bbd05644e53adbf27508',
{ "genre": "comedy" }, 'title': 'barbie'
{ "release_year": 2023 } 'genre': [
] 'comedy',
}) 'fantasy',
'adventure'
# Equivalent! ],
mov.find_one({ 'rating': 6.9,
"genre": "comedy", 'release_year': 2023,
"release_year": 2023, }
})
INTRODUCTION TO MONGODB IN PYTHON
Combining queries with $or
curs = [Link]({ [
"$or": [ {
{ "genre": "comedy" }, '_id': '6824bbd05644e53adbf274ff',
{ "release_year": 2023 } 'title': 'superbad',
] 'genre': ['comedy', 'teen'],
}) 'rating': 7.6,
'release_year': 2007
list(curs) },
...
{
'_id': '6824bbd05644e53adbf2750c',
'title': 'm3gan',
'genre': ['horror', 'sci-fi', 'thriller'],
'release_year': 2023,
'rating': 6.4,
'won_oscar': False
}
...
]
INTRODUCTION TO MONGODB IN PYTHON
Let's practice!
INTRODUCTION TO MONGODB IN PYTHON
Query options in
MongoDB
INTRODUCTION TO MONGODB IN PYTHON
Filip Schouwenaars
Machine Learning Researcher
Limiting the number of results
from pymongo import MongoClient from pymongo import MongoClient
client = MongoClient() client = MongoClient()
mov = [Link] mov = [Link]
# Get all movies (cursor) # Get five movies (cursor)
all_movies = [Link]() limit_movies = [Link]().limit(5)
# How many? # How many?
len(list(all_movies)) len(list(limit_movies))
41 5
INTRODUCTION TO MONGODB IN PYTHON
Sorting in ascending and descending order
# By title, ascending (A-Z) # By release year, descending (high to low)
t_sort = [Link]().sort("title", 1) ry_sort = [Link]().sort("release_year", -1)
list(t_sort) list(ry_sort)
[ [
{ {
'title': '12 angry men', 'title': 'dune: part two',
'release_year': 1957, 'release_year': 2024,
... ...
}, },
{ {
'title': 'a beautiful mind', 'title': 'barbie',
'release_year': 2001, 'release_year': 2023,
... ...
}, },
... ...
] ]
INTRODUCTION TO MONGODB IN PYTHON
Counting matching documents
from pymongo import MongoClient
client = MongoClient()
mov = [Link]
# Count action movies with find()
len(list([Link]({ "genre": "action" })))
16
# Count action movies, but better
mov.count_documents({ "genre": "action" })
16
INTRODUCTION TO MONGODB IN PYTHON
Using projection to shape your results
movies = [Link]({}, { movies = [Link]({}, {
"title": 1, "rating": 1, "title": 1, "rating": 1, "_id": 0
}) })
list(movies) list(movies)
[ [
{'_id': '6...f', 'rating': 7.6, 'title': 'superbad'}, {'title': 'superbad', 'rating': 7.6},
{'_id': '6...0', 'rating': 8.6, 'title': 'interstellar'}, {'title': 'interstellar', 'rating': 8.6},
{'_id': '6...1', 'rating': 8.8, 'title': 'inception'}, {'title': 'inception', 'rating': 8.8},
{'_id': '6...2', 'rating': 9.2, 'title': 'the godfather'}, {'title': 'the godfather', 'rating': 9.2},
{'_id': '6...3', 'rating': 8, 'title': 'the revenant'}, {'title': 'the revenant', 'rating': 8},
... ...
] ]
INTRODUCTION TO MONGODB IN PYTHON
Let's practice!
INTRODUCTION TO MONGODB IN PYTHON