CS377: Database Design - Create, Read, Update, and Delete (CRUD) with NoSQL
Activity Goals
The goals of this activity are:- To explore the core operations on an unstructured data store: create, update, read, and delete (CRUD)
- To be able to implement each of the CRUD operations on a NoSQL database in Python
Supplemental Reading
Feel free to visit these resources for supplemental background reading material.The Activity
Directions
Consider the activity models and answer the questions provided. First reflect on these questions on your own briefly, before discussing and comparing your thoughts with your group. Appoint one member of your group to discuss your findings with the class, and the rest of the group should help that member prepare their response. Answer each question individually from the activity, and compare with your group to prepare for our whole-class discussion. After class, think about the questions in the reflective prompt and respond to those individually in your notebook. Report out on areas of disagreement or items for which you and your group identified alternative approaches. Write down and report out questions you encountered along the way for group discussion.Model 1: Create
Questions
- Investigate how to create an index in a MongoDB collection using PyMongo.
- What kind of data do you think is best suited for a relational database, and what kind might be better suited for this non-relational approach? As a group, write down three examples of each. Let's compare with the class and see who has the most unique ideas!
Model 2: Read
Questions
- Modify the above program to insert records into a datastore and query them, iterating over the results.
- Which statements search for items according to their value?
- How is the sorted cursor sorted?
- Investigate how to search for records that contain a key called
val3
in your collection. Hint: consider the$exists
clause. - By default, multiple items in a
find
are combined using a booleanAND
operation. Investigate how to search by combining search terms with anOR
. Hint: Consider the$or
clause.
Embedded Code Environment
You can try out some code examples in this embedded development environment! To share this with someone else, first have one member of your group make a small change to the file, then click "Open in Repl.it". Log into your Repl.it account (or create one if needed), and click the "Share" button at the top right. Note that some embedded Repl.it projects have multiple source files; you can see those by clicking the file icon on the left navigation bar of the embedded code frame. Share the link that opens up with your group members. Remember only to do this for partner/group activities!Model 3: Update
Questions
- What do you think
$set
and$unset
do? - What would the
update
without themulti
modifier do? - What does
$push
and$rename
do? - What does the
findAndModify
statement do in this example? - What do you think would happen if the
query
parameter is set to{}
in thefind
andupdate
statements above?
Model 4: Delete
Questions
- What is an alternative to removing items from a datastore so that you never actually delete anything, while omitting "removed" records from your queries?
- Do you think the
query
parameter works withremove
? Try it out, and remove all items from the collection whoseval1
value is greater than 42!
Model 5: MongoDB Reference Guide
CRUD Operations with NoSQL
NoSQL databases are a popular choice for managing large volumes of unstructured and semi-structured data. Unlike traditional relational databases, NoSQL databases offer flexible data models and scalable architecture. CRUD operations (Create, Read, Update, Delete) are fundamental to working with any database, including NoSQL databases. In this report, we will explore CRUD operations with NoSQL databases, focusing on MongoDB as an example.
1. Create
To create a new document in a NoSQL database, we use the insert_one()
or insert_many()
methods in the PyMongo library. Here’s an example:
from pymongo import MongoClient
# Connect to MongoDB
client = MongoClient()
db = client.mydatabase
# Create a new document
new_document = {"name": "John Doe", "age": 25, "email": "johndoe@example.com"}
result = db.mycollection.insert_one(new_document)
# Print the inserted document's ID
print("Document inserted with ID:", result.inserted_id)
2. Read
To retrieve data from a NoSQL database, we use the find()
method in PyMongo. We can pass a query object to filter the data. Here’s an example:
from pymongo import MongoClient
# Connect to MongoDB
client = MongoClient()
db = client.mydatabase
# Retrieve data
data = db.mycollection.find({"age": {"$gte": 18}})
# Print the retrieved documents
for document in data:
print(document)
3. Update
To update existing documents in a NoSQL database, we use the update_one()
or update_many()
methods in PyMongo. Here’s an example:
from pymongo import MongoClient
# Connect to MongoDB
client = MongoClient()
db = client.mydatabase
# Update a document
query = {"name": "John Doe"}
update = {"$set": {"age": 30, "email": "johndoe@example.com"}}
result = db.mycollection.update_one(query, update)
# Print the number of modified documents
print("Documents modified:", result.modified_count)
4. Delete
To remove documents from a NoSQL database, we use the delete_one()
or delete_many()
methods in PyMongo. Here’s an example:
from pymongo import MongoClient
# Connect to MongoDB
client = MongoClient()
db = client.mydatabase
# Delete a document
query = {"name": "John Doe"}
result = db.mycollection.delete_one(query)
# Print the number of deleted documents
print("Documents deleted:", result.deleted_count)