MONGO Code Drills:

Introductory Drills in MongoDB Compass for New Learners

Here are six introductory drills designed for new users to familiarize themselves with MongoDB Compass and basic CRUD (Create, Read, Update, Delete) operations on JSON in a MongoDB database.

Pre-Drill Setup

Before starting the drills:
Download and install MongoDB Compass.
Connect MongoDB Compass to a local MongoDB instance or Atlas cluster with the appropriate connection string.

Drill 1: Creating a Database and a Collection

Learn how to create a new database.
Learn how to create a collection within the database.
Click the "Create Database" button on the Databases tab.
Enter learning_mongo as the Database Name.
Enter users as the Collection Name.
Click "Create Database."

Drill 2: Inserting Documents into a Collection

Insert a single document into your collection.
Insert multiple documents into your collection.
Navigate to your learning_mongo database and users collection.
Click the "Insert Document" button.
In the opened editor, insert a single document with fields such as name, email, and age.

"name": "John Doe",
"email": "",
"age": 28
Click "Insert".
Insert multiple documents by clicking "Insert Document" again, adding a few more user entries in the array.

Drill 3: Finding Documents in a Collection

Learn how to use the find operation.
Learn to use queries to filter results.
Click on the "Find" tab in your users collection.
Try a blank find query {} to retrieve all documents.
Execute a find query with a condition, like finding users with an age greater than 25 using {"age": {$gt: 25}}.

Drill 4: Updating Documents

Use the update operation to modify an existing document.
Learn to use update operators like $set and $unset.
Find a user with the name "John Doe".
Click on the "Edit Document" button of the found document.
Modify John's age to 30 using the $set operator: {"$set": {"age": 30}}.
Update the user's document by removing the email field using the $unset operator.

Drill 5: Deleting Documents

Learn to delete a single document.
Learn to delete multiple documents matching a filter.
Delete the previously updated John Doe using its unique _id by clicking on the "Delete Document" button.
To delete multiple documents, use the filter { "age": {"$lt": 18}} to match all documents representing users under 18, and click "Delete Documents".

Drill 6: Creating Indexes to Speed up Queries

Learn how to create an index on a field to improve query performance.
Click on the "Indexes" tab while in your users collection.
Create a new index on the email field, which you expect to query frequently.
Use { "email": 1 } to create an ascending index.
By completing these six drills, new learners will become accustomed to the basic functions of MongoDB Compass and understand how to perform simple CRUD operations on a MongoDB database. As they progress, they can tackle more complex queries, aggregation operations, and explore schema validation rules to complement their MongoDB skills.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
) instead.