Training material for MongoDb Cross-Team Training Day
Use pipenv
to install the necessary dependencies.
$ pipenv install --dev
Start the pipenv
shell with the following command
$ pipenv shell
Create a .env
in the root of the repository with the following value
export MONGODB_URI=mongodb+srv://<username:<password>@<your-cluster>.mongodb.net/myTestDatabase?retryWrites=true&w=majority
The data_modeling
directory contains the code for the data-modeling lesson. To run the application
use the following command
$ uvicorn data_modeling.main:app --reload
This repo contains a Postman collection for testing the API called Book API.postman_collection.json
.
This file can be imported into the Postman application.
The update endpoint allows tags
to be updated. Right now it will overwrite the existing tags.
Update the update_book
function to add new tags without over-writing the old.
We want to know more information about the author. Convert the author property to a nested document including the following fields using a Pydantic model
- bio - a text field where some information about the author can be stored
- rating - a numerical field where users can rate the author with 1 - 10 stars in increments of 0.5
- first_name - the author's first name
- last_name - the author's last name
Note: When updating a book, the author information should not be over-written.
We want to add chapters to the book with a chapter title. Use a Pydantic model with the following fields and add the chapter to the Book entity.
- chapter_title - a text field of the chapter title. (i.e. Foreword, 1, Index)
- synopsis - a text field with a summary of the chapter in a few sentences.
- pages - the number of pages in the chapter
MongoDb 4.x supports ACID compliant transactions. To use transactions you will need to use the session object in a with-statement. Documentation
The transactions
directory contains the code for the transactions lesson. To run the application
use the following command
$ uvicorn transactions.main:app --reload
This repo contains a Postman collection for testing the API called Webshop API.postman_collection.json
.
This file can be imported into the Postman application.
The customer has two properties which need to be updated and saved back to the customers
collection. Add an appropriate
update statement.
Use the data below to insert a new customer and a new order. Note: when inserting the customer, the id of the new customer will be returned in the API response. Use this id when inserting a new order.
Sample Customer Document
{
"first_name": "John",
"last_name": "Doe",
"mobile_mobile": "0611111111",
"email_address": "[email protected]",
"total_purchased": 0,
"total_orders": 0
}
Sample Order Document
{
"customer_id": "<insert customer id>",
"items": [
{
"sku": "1111",
"qty": 1,
"price": 5.25
},
{
"sku": "2222",
"qty": 2,
"price": 11.5
}
],
"total": 0
}
Implement a stock keeping collection which will keep the quantity of a given sku. When an item is sold the stock quantity should be decreased when a new order is created.
Sample Stock Document
{
"sku": "1111",
"qty": 100
}
MongoDb like most database can be indexed to improve query performance. MongoDb comes with tools to access query performace such as the Explain function.
For more information about indexes see the documentation below
MongoDb Documentation PyMongo Documentation
The customer collection will be searched by the customer's first name and last name. Create an index on these fields. Hint: Use a compound index.
First, run the query before applying the index and copy the results out.
db.customers.find({"first_name": "John", "last_name": "Doe"}).explain()
Next, create the index following the index creation documentation.
Once the index is created, use the Explain function to see which index was used when running the query below.
db.customers.find({"first_name": "John", "last_name": "Doe"}).explain()
Compare the Explain results of running the query with and without an index. What is different?