Practical Lab: Create a Repeatable MongoDB Environment
In this lab, create a database, configure a collection, insert documents, and verify that the environment works. You can use Atlas, a local MongoDB server, or Docker.
Step 1: Connect to MongoDB
Connect using mongosh and verify the connection:
db.runCommand({ ping: 1 });
Step 2: Select a dedicated practice database
use mongodb_environment_lab
Step 3: Create a collection with validation
db.createCollection("developers", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["name", "email", "experienceYears"],
properties: {
name: {
bsonType: "string",
minLength: 1
},
email: {
bsonType: "string",
minLength: 3
},
experienceYears: {
bsonType: ["int", "long", "double", "decimal"],
minimum: 0
},
skills: {
bsonType: "array",
items: {
bsonType: "string"
}
},
location: {
bsonType: "object",
properties: {
city: { bsonType: "string" },
country: { bsonType: "string" }
}
}
}
}
},
validationLevel: "strict",
validationAction: "error"
});
Step 4: Insert valid documents
db.developers.insertMany([
{
name: "Arun",
email: "arun@example.com",
experienceYears: 3,
skills: ["JavaScript", "Node.js", "MongoDB"],
location: {
city: "Chennai",
country: "India"
}
},
{
name: "Meera",
email: "meera@example.com",
experienceYears: 5,
skills: ["React", "TypeScript", "MongoDB"],
location: {
city: "Bengaluru",
country: "India"
}
}
]);
Step 5: Read and inspect documents
db.developers.find();
db.getCollectionNames();
db.getCollectionInfos();
Step 6: Test validation
Attempt to insert a document missing email:
db.developers.insertOne({
name: "Invalid Example",
experienceYears: 2
});
The operation should fail because email is required. Run this only as an intentional validation test in your practice environment.
Step 7: Verify nested data
db.developers.find({ "location.city": "Chennai" });
This query uses dot notation to find developers whose nested city field is Chennai.
Step 8: Count documents
db.developers.countDocuments({});
If both valid documents were inserted successfully and no additional documents were added, the result should be 2.
Step 9: Check environment configuration
If you are using a Node.js application, confirm that the connection string is supplied through the server-side MONGODB_URI environment variable. Verify its presence without printing its value.
Step 10: Clean up if needed
Only if you want to remove this disposable practice collection:
db.developers.drop();
Dropping the collection removes its documents and collection configuration. Do not execute this command against a production collection.
Lab challenges
- Create another collection called
courses. - Insert three course documents with different difficulty levels.
- Find courses with a particular difficulty.
- Create a validator that requires
titleandlevel. - Intentionally attempt an invalid insert and explain the error.
- Explain why a successful ping does not guarantee write permission.
- Describe how you would prevent a database URI from reaching browser code.
- Explain how a Docker named volume differs from a container.
- Identify the first three things you would check after a server selection timeout.
- Explain why a development database should be separate from production.
db.runCommand({ ping: 1 });
print("Database:", db.getName());
print("Collections:");
printjson(db.getCollectionNames());
print("Developer count:", db.developers.countDocuments({}));
print("Chennai developers:");
printjson(
db.developers.find({ "location.city": "Chennai" }).toArray()
);- A working environment requires connectivity, correct authentication, and appropriate permissions.
- Collection validation can enforce required fields and acceptable BSON types.
- Database and collection metadata can be inspected using mongosh.
- Nested documents can be queried using dot notation.
- Practice destructive operations only in disposable development environments.
In the lab, why does the invalid developer insert fail?