Lecture 6: CRUD operations in MongoDB#
Learning objectives#
By the end of this lecture, students should understand:
Describe the use cases for the MongoDB Query Language (MQL).
Perform basic CRUD operations using MQL.
Execute queries using MQL find() and delete().
Use the most common MQL query operators to optimize searches.
Lecture Notes#
Introduction#
CRUD operations are the basic operations you can perform on a database. CRUD stands for Create, Read, Update, and Delete. In MongoDB, these operations can be performed using various methods provided by the PyMongo library.
MQL is designed for single collection queries and it is typically used for creating, reading, updating, or deletion (CRUD) operations.
MQL query operators:
Comparison
Logical
Element
Array
Evaluation
from pymongo import MongoClient # import mongo client to connect
import json # import json to load credentials
import urllib.parse
# load credentials from json file
with open('credentials_mongodb.json') as f:
login = json.load(f)
# assign credentials to variables
username = login['username']
password = urllib.parse.quote(login['password'])
host = login['host']
url = "mongodb+srv://{}:{}@{}/?retryWrites=true&w=majority".format(username, password, host)
# connect to the database
client = MongoClient(url)
# drop database books and students if they exist
client.drop_database('library')
# create database and collection
db = client["library"]
collection = db["books"]
# add sample books to the collection
sample_books = [ {"title": "The Great Gatsby", "author": "F. Scott Fitzgerald", "year": 1925},
{"title": "A Farewell to Arms", "author": "Ernest Hemingway", "year": 1929},
{"title": "The Catcher in the Rye", "author": "J.D. Salinger", "year": 1951},
{"title": "The Grapes of Wrath", "author": "John Steinbeck", "year": 1939},
{"title": "To Kill a Mockingbird", "author": "Harper Lee", "year": 1960},
{"title": "1984", "author": "George Orwell", "year": 1949},
{"title": "Animal Farm", "author": "George Orwell", "year": 1945}]
sample_books
[{'title': 'The Great Gatsby', 'author': 'F. Scott Fitzgerald', 'year': 1925},
{'title': 'A Farewell to Arms', 'author': 'Ernest Hemingway', 'year': 1929},
{'title': 'The Catcher in the Rye', 'author': 'J.D. Salinger', 'year': 1951},
{'title': 'The Grapes of Wrath', 'author': 'John Steinbeck', 'year': 1939},
{'title': 'To Kill a Mockingbird', 'author': 'Harper Lee', 'year': 1960},
{'title': '1984', 'author': 'George Orwell', 'year': 1949},
{'title': 'Animal Farm', 'author': 'George Orwell', 'year': 1945}]
# insert sample books to the collection
collection.insert_many(sample_books)
InsertManyResult([ObjectId('66e9b117529d9b11d569aab3'), ObjectId('66e9b117529d9b11d569aab4'), ObjectId('66e9b117529d9b11d569aab5'), ObjectId('66e9b117529d9b11d569aab6'), ObjectId('66e9b117529d9b11d569aab7'), ObjectId('66e9b117529d9b11d569aab8'), ObjectId('66e9b117529d9b11d569aab9')], acknowledged=True)
1. Find (Read)#
The find
method is used to query documents from a collection.
Syntax#
collection.find(query, projection)
query
: A dictionary specifying the criteria for the query.projection
: A dictionary specifying the fields to include or exclude.
Example#
# Find all documents, this will return a cursor
collection.find()
<pymongo.cursor.Cursor at 0x104fff7a0>
# print all documents
for book in collection.find():
print(book)
{'_id': ObjectId('66e9b117529d9b11d569aab3'), 'title': 'The Great Gatsby', 'author': 'F. Scott Fitzgerald', 'year': 1925}
{'_id': ObjectId('66e9b117529d9b11d569aab4'), 'title': 'A Farewell to Arms', 'author': 'Ernest Hemingway', 'year': 1929}
{'_id': ObjectId('66e9b117529d9b11d569aab5'), 'title': 'The Catcher in the Rye', 'author': 'J.D. Salinger', 'year': 1951}
{'_id': ObjectId('66e9b117529d9b11d569aab6'), 'title': 'The Grapes of Wrath', 'author': 'John Steinbeck', 'year': 1939}
{'_id': ObjectId('66e9b117529d9b11d569aab7'), 'title': 'To Kill a Mockingbird', 'author': 'Harper Lee', 'year': 1960}
{'_id': ObjectId('66e9b117529d9b11d569aab8'), 'title': '1984', 'author': 'George Orwell', 'year': 1949}
{'_id': ObjectId('66e9b117529d9b11d569aab9'), 'title': 'Animal Farm', 'author': 'George Orwell', 'year': 1945}
# print all documents without _id
for book in collection.find({}, {"_id": 0, "title": 0}):
print(book)
{'author': 'F. Scott Fitzgerald', 'year': 1925}
{'author': 'Ernest Hemingway', 'year': 1929}
{'author': 'J.D. Salinger', 'year': 1951}
{'author': 'John Steinbeck', 'year': 1939}
{'author': 'Harper Lee', 'year': 1960}
{'author': 'George Orwell', 'year': 1949}
{'author': 'George Orwell', 'year': 1945}
# Find documents with a specific condition
specific_books = collection.find({"year": {"$gt": 1950}})
# Print the results
for book in specific_books:
print(book)
{'_id': ObjectId('66e9b117529d9b11d569aab5'), 'title': 'The Catcher in the Rye', 'author': 'J.D. Salinger', 'year': 1951}
{'_id': ObjectId('66e9b117529d9b11d569aab7'), 'title': 'To Kill a Mockingbird', 'author': 'Harper Lee', 'year': 1960}
# Find documents with a specific condition, and return only the title
specific_books = collection.find({"year": {"$gt": 1950}}, {"title": 1, "year": 1, "_id": 0})
# Print the results
for book in specific_books:
print(book)
{'title': 'The Catcher in the Rye', 'year': 1951}
{'title': 'To Kill a Mockingbird', 'year': 1960}
2. Insert (Create)#
The insert_one
and insert_many
methods are used to insert documents into a collection.
Syntax#
collection.insert_one(document)
collection.insert_many(documents)
document
: A dictionary representing a single document.documents
: A list of dictionaries representing multiple documents.
Example#
# Insert a single document
new_book = {
"title": "Brave New World",
"author": "Aldous Huxley",
"published_year": 1932,
"isbn": "978-0-06-085052-4",
"price": 11.99
}
collection.insert_one(new_book)
InsertOneResult(ObjectId('66e9b353529d9b11d569aaba'), acknowledged=True)
# Insert multiple documents
new_books = [
{
"title": "The Road",
"author": "Cormac McCarthy",
"published_year": 2006,
"isbn": "978-0-307-38789-9",
"price": 12.99
},
{
"title": "The Handmaid's Tale",
"author": "Margaret Atwood",
"published_year": 1985,
"isbn": "978-0-385-49081-8",
"price": 9.99
}
]
collection.insert_many(new_books)
InsertManyResult([ObjectId('66e9b37f529d9b11d569aabb'), ObjectId('66e9b37f529d9b11d569aabc')], acknowledged=True)
# check if the documents were inserted
for book in collection.find():
print(book)
{'_id': ObjectId('66e9b117529d9b11d569aab3'), 'title': 'The Great Gatsby', 'author': 'F. Scott Fitzgerald', 'year': 1925}
{'_id': ObjectId('66e9b117529d9b11d569aab4'), 'title': 'A Farewell to Arms', 'author': 'Ernest Hemingway', 'year': 1929}
{'_id': ObjectId('66e9b117529d9b11d569aab5'), 'title': 'The Catcher in the Rye', 'author': 'J.D. Salinger', 'year': 1951}
{'_id': ObjectId('66e9b117529d9b11d569aab6'), 'title': 'The Grapes of Wrath', 'author': 'John Steinbeck', 'year': 1939}
{'_id': ObjectId('66e9b117529d9b11d569aab7'), 'title': 'To Kill a Mockingbird', 'author': 'Harper Lee', 'year': 1960}
{'_id': ObjectId('66e9b117529d9b11d569aab8'), 'title': '1984', 'author': 'George Orwell', 'year': 1949}
{'_id': ObjectId('66e9b117529d9b11d569aab9'), 'title': 'Animal Farm', 'author': 'George Orwell', 'year': 1945}
{'_id': ObjectId('66e9b353529d9b11d569aaba'), 'title': 'Brave New World', 'author': 'Aldous Huxley', 'published_year': 1932, 'isbn': '978-0-06-085052-4', 'price': 11.99}
{'_id': ObjectId('66e9b37f529d9b11d569aabb'), 'title': 'The Road', 'author': 'Cormac McCarthy', 'published_year': 2006, 'isbn': '978-0-307-38789-9', 'price': 12.99}
{'_id': ObjectId('66e9b37f529d9b11d569aabc'), 'title': "The Handmaid's Tale", 'author': 'Margaret Atwood', 'published_year': 1985, 'isbn': '978-0-385-49081-8', 'price': 9.99}
3. Update#
The update_one
and update_many
methods are used to update documents in a collection.
Syntax#
collection.update_one(filter, update)
collection.update_many(filter, update)
filter
: A dictionary specifying the criteria for the update.update
: A dictionary specifying the fields to update.
Example#
# Update a single document
collection.update_one(
{"title": "1984"},
{"$set": {"price": 8.49, "year": 2000}}
)
UpdateResult({'n': 1, 'electionId': ObjectId('7fffffff00000000000000cb'), 'opTime': {'ts': Timestamp(1726592254, 20), 't': 203}, 'nModified': 1, 'ok': 1.0, '$clusterTime': {'clusterTime': Timestamp(1726592254, 20), 'signature': {'hash': b'[4%;\xf6=\xedY\xb4\xbf\xeapc\xab\xfco\x0c?\x0fG', 'keyId': 7381891571605569538}}, 'operationTime': Timestamp(1726592254, 20), 'updatedExisting': True}, acknowledged=True)
collection.find_one({"title": "1984"})
{'_id': ObjectId('66e9b117529d9b11d569aab8'),
'title': '1984',
'author': 'George Orwell',
'year': 2000,
'price': 8.49}
for book in collection.find({"author": "George Orwell"}, {"_id": 0}):
print(book)
{'title': '1984', 'author': 'George Orwell', 'year': 2000, 'price': 8.49}
{'title': 'Animal Farm', 'author': 'George Orwell', 'year': 1945}
# Update multiple documents
collection.update_many(
{"author": "George Orwell"},
{"$set": {"genre": "Dystopian"}}
)
UpdateResult({'n': 2, 'electionId': ObjectId('7fffffff00000000000000cb'), 'opTime': {'ts': Timestamp(1726592400, 6), 't': 203}, 'nModified': 2, 'ok': 1.0, '$clusterTime': {'clusterTime': Timestamp(1726592400, 6), 'signature': {'hash': b'\xc5Y7\xe2\x9a\xf6]\xb9\x80\xb5\x08c\xca^m5d\xeaf;', 'keyId': 7381891571605569538}}, 'operationTime': Timestamp(1726592400, 6), 'updatedExisting': True}, acknowledged=True)
# check if the documents were updated
# print book with title "1984"
print(collection.find_one({"title": "1984"}))
{'_id': ObjectId('66e8a0597a895c83fa099dec'), 'title': '1984', 'author': 'George Orwell', 'year': 1949, 'price': 8.49, 'genre': 'Dystopian'}
# print all books with author "George Orwell"
for book in collection.find({"author": "George Orwell"}, {"_id": 0}):
print(book)
{'title': '1984', 'author': 'George Orwell', 'year': 2000, 'price': 8.49, 'genre': 'Dystopian'}
{'title': 'Animal Farm', 'author': 'George Orwell', 'year': 1945, 'genre': 'Dystopian'}
4. Delete#
The delete_one
and delete_many
methods are used to delete documents from a collection.
Syntax#
collection.delete_one(filter)
collection.delete_many(filter)
filter
: A dictionary specifying the criteria for the deletion.
Example#
# Delete a single document
collection.delete_one({"title": "1984"})
DeleteResult({'n': 1, 'electionId': ObjectId('7fffffff00000000000000cb'), 'opTime': {'ts': Timestamp(1726592514, 36), 't': 203}, 'ok': 1.0, '$clusterTime': {'clusterTime': Timestamp(1726592514, 36), 'signature': {'hash': b'\xe9i\x06\xd4QV?3n\xf1\xe5\xc7_~\x93\xb3,\xac\xeb{', 'keyId': 7381891571605569538}}, 'operationTime': Timestamp(1726592514, 36)}, acknowledged=True)
collection.find_one({"title": "1984"})
# Delete multiple documents
collection.delete_many({"author": "George Orwell"})
DeleteResult({'n': 1, 'electionId': ObjectId('7fffffff00000000000000cb'), 'opTime': {'ts': Timestamp(1726592534, 5), 't': 203}, 'ok': 1.0, '$clusterTime': {'clusterTime': Timestamp(1726592534, 5), 'signature': {'hash': b'di7\xf6\xcf\xfe]\x9e-\x13r\xe7\xa1*1\x9c\x82a\xcd\x84', 'keyId': 7381891571605569538}}, 'operationTime': Timestamp(1726592534, 5)}, acknowledged=True)
for book in collection.find({"author": "George Orwell"}, {"_id": 0}):
print(book)
# check if the documents were deleted
# print book with title "1984"
print(collection.find_one({"title": "1984"}))
None
# print all books with author "George Orwell"
for book in collection.find({"author": "George Orwell"}):
print(book)
Delete a field
collection.find_one({"author": "J.D. Salinger"})
{'_id': ObjectId('66e9b117529d9b11d569aab5'),
'title': 'The Catcher in the Rye',
'author': 'J.D. Salinger',
'year': 1951}
# Update multiple documents
collection.update_many(
{"author": "J.D. Salinger"},
{"$unset": {"year": ""}}
)
UpdateResult({'n': 1, 'electionId': ObjectId('7fffffff00000000000000cb'), 'opTime': {'ts': Timestamp(1726592714, 6), 't': 203}, 'nModified': 1, 'ok': 1.0, '$clusterTime': {'clusterTime': Timestamp(1726592714, 6), 'signature': {'hash': b'E\xea\x08a\x9c|\xc3\x879\xe6\xca\x91g\xa4\xed\xa6\x85^*i', 'keyId': 7381891571605569538}}, 'operationTime': Timestamp(1726592714, 6), 'updatedExisting': True}, acknowledged=True)
collection.find_one({"author": "J.D. Salinger"})
{'_id': ObjectId('66e9b117529d9b11d569aab5'),
'title': 'The Catcher in the Rye',
'author': 'J.D. Salinger'}
5. Operators#
MongoDB provides various operators to perform complex queries and updates.
Common Operators#
$gt
: Greater than$lt
: Less than$gte
: Greater than or equal to$lte
: Less than or equal to$eq
: Equal to$ne
: Not equal to$in
: In a list$nin
: Not in a list$set
: Set a field to a value$unset
: Remove a field
Example#
# print all documents
for book in collection.find():
print(book)
{'_id': ObjectId('66e8a0597a895c83fa099de7'), 'title': 'The Great Gatsby', 'author': 'F. Scott Fitzgerald', 'year': 1925}
{'_id': ObjectId('66e8a0597a895c83fa099de8'), 'title': 'A Farewell to Arms', 'author': 'Ernest Hemingway', 'year': 1929}
{'_id': ObjectId('66e8a0597a895c83fa099de9'), 'title': 'The Catcher in the Rye', 'author': 'J.D. Salinger', 'year': 1951}
{'_id': ObjectId('66e8a0597a895c83fa099dea'), 'title': 'The Grapes of Wrath', 'author': 'John Steinbeck', 'year': 1939}
{'_id': ObjectId('66e8a0597a895c83fa099deb'), 'title': 'To Kill a Mockingbird', 'author': 'Harper Lee', 'year': 1960}
{'_id': ObjectId('66e8a05d7a895c83fa099dee'), 'title': 'Brave New World', 'author': 'Aldous Huxley', 'published_year': 1932, 'isbn': '978-0-06-085052-4', 'price': 11.99}
{'_id': ObjectId('66e8a05d7a895c83fa099def'), 'title': 'The Road', 'author': 'Cormac McCarthy', 'published_year': 2006, 'isbn': '978-0-307-38789-9', 'price': 12.99}
{'_id': ObjectId('66e8a05d7a895c83fa099df0'), 'title': "The Handmaid's Tale", 'author': 'Margaret Atwood', 'published_year': 1985, 'isbn': '978-0-385-49081-8', 'price': 9.99}
# Find documents with price greater than 10
expensive_books = collection.find({"price": {"$gt": 10}})
for book in expensive_books:
print(book)
{'_id': ObjectId('66e9b353529d9b11d569aaba'), 'title': 'Brave New World', 'author': 'Aldous Huxley', 'published_year': 1932, 'isbn': '978-0-06-085052-4', 'price': 11.99}
{'_id': ObjectId('66e9b37f529d9b11d569aabb'), 'title': 'The Road', 'author': 'Cormac McCarthy', 'published_year': 2006, 'isbn': '978-0-307-38789-9', 'price': 12.99}
author_J = collection.find({"author": {"$regex": "^J"}})
for book in author_J:
print(book)
{'_id': ObjectId('66e9b117529d9b11d569aab5'), 'title': 'The Catcher in the Rye', 'author': 'J.D. Salinger'}
{'_id': ObjectId('66e9b117529d9b11d569aab6'), 'title': 'The Grapes of Wrath', 'author': 'John Steinbeck', 'year': 1939}
# Delete documents where author starts with "J"
collection.delete_many({"author": {"$regex": "^J"}})
for book in collection.find():
print(book)
{'_id': ObjectId('66e9b117529d9b11d569aab3'), 'title': 'The Great Gatsby', 'author': 'F. Scott Fitzgerald', 'year': 1925}
{'_id': ObjectId('66e9b117529d9b11d569aab4'), 'title': 'A Farewell to Arms', 'author': 'Ernest Hemingway', 'year': 1929}
{'_id': ObjectId('66e9b117529d9b11d569aab7'), 'title': 'To Kill a Mockingbird', 'author': 'Harper Lee', 'year': 1960}
{'_id': ObjectId('66e9b353529d9b11d569aaba'), 'title': 'Brave New World', 'author': 'Aldous Huxley', 'published_year': 1932, 'isbn': '978-0-06-085052-4', 'price': 11.99}
{'_id': ObjectId('66e9b37f529d9b11d569aabb'), 'title': 'The Road', 'author': 'Cormac McCarthy', 'published_year': 2006, 'isbn': '978-0-307-38789-9', 'price': 12.99}
{'_id': ObjectId('66e9b37f529d9b11d569aabc'), 'title': "The Handmaid's Tale", 'author': 'Margaret Atwood', 'published_year': 1985, 'isbn': '978-0-385-49081-8', 'price': 9.99}
author_end_with_y = collection.find({"author": {"$regex": "y$"}})
for book in author_end_with_y:
print(book)
{'_id': ObjectId('66e9b117529d9b11d569aab4'), 'title': 'A Farewell to Arms', 'author': 'Ernest Hemingway', 'year': 1929}
{'_id': ObjectId('66e9b353529d9b11d569aaba'), 'title': 'Brave New World', 'author': 'Aldous Huxley', 'published_year': 1932, 'isbn': '978-0-06-085052-4', 'price': 11.99}
{'_id': ObjectId('66e9b37f529d9b11d569aabb'), 'title': 'The Road', 'author': 'Cormac McCarthy', 'published_year': 2006, 'isbn': '978-0-307-38789-9', 'price': 12.99}
# Show documents where year is between 1920 and 1930
books_between_1930_and_1950 = collection.find({"year": {"$gte": 1920, "$lte": 1930}})
for book in books_between_1930_and_1950:
print(book)
{'_id': ObjectId('66e8a0597a895c83fa099de7'), 'title': 'The Great Gatsby', 'author': 'F. Scott Fitzgerald', 'year': 1925}
{'_id': ObjectId('66e8a0597a895c83fa099de8'), 'title': 'A Farewell to Arms', 'author': 'Ernest Hemingway', 'year': 1929}
Conclusion#
These are the basic CRUD operations you can perform on a MongoDB database using PyMongo. Understanding these operations is essential for managing and manipulating data in MongoDB.
Supplemental materials#
Read more here: https://www.mongodb.com/docs/manual/crud/