Share
Explore

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

Objectives:
Learn how to create a new database.
Learn how to create a collection within the database.
Actions:
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

Objectives:
Insert a single document into your collection.
Insert multiple documents into your collection.
Actions:
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.
Example:

{
"name": "John Doe",
"email": "john.doe@example.com",
"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

Objectives:
Learn how to use the find operation.
Learn to use queries to filter results.
Actions:
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

Objectives:
Use the update operation to modify an existing document.
Learn to use update operators like $set and $unset.
Actions:
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

Objectives:
Learn to delete a single document.
Learn to delete multiple documents matching a filter.
Actions:
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

Objectives:
Learn how to create an index on a field to improve query performance.
Actions:
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 (
CtrlP
) instead.