MongoDB Crash Course
Table of Contents
Introduction
This tutorial serves as a comprehensive guide to MongoDB, a popular NoSQL database that is easy to use and flexible for various projects. We will cover essential concepts, including basic commands, data manipulation, querying, updating, and deleting records. By the end of this guide, you'll have a solid understanding of MongoDB's core functionalities.
Chapter 1: Basic Commands
To get started with MongoDB, ensure you have it installed on your system. Follow these steps:
-
Install MongoDB:
- Visit the MongoDB Community Edition download page.
- Choose your operating system and follow the installation instructions.
-
Install MongoDB Shell (sh):
- Download the MongoDB Shell appropriate for your operating system.
- Follow the installation steps provided.
-
Open the Terminal:
- Launch your terminal and type
sh
to enter the MongoDB Shell.
- Launch your terminal and type
-
Basic Commands:
- To view existing databases, type:
show dbs
- To switch to an existing database or create a new one, use:
use appdb
- To view collections within the current database, type:
show collections
- To delete a database, use:
db.dropDatabase()
- To clear the terminal screen, type:
cls
- To exit the shell, simply type:
exit
- To view existing databases, type:
Chapter 2: Insert Commands
Inserting data into MongoDB is straightforward. Follow these steps:
-
Insert a Single Record:
- To insert a user, use:
db.users.insertOne({ name: "John" })
- To insert a user, use:
-
View Records:
- To see the inserted records, type:
db.users.find()
- To see the inserted records, type:
-
Insert Multiple Records:
- To insert multiple users at once, use:
db.users.insertMany([{ name: "Jill" }, { name: "Mike" }])
- To insert multiple users at once, use:
-
Insert Complex Documents:
- You can insert documents with nested objects and arrays:
db.users.insertOne({ name: "Sally", age: 19, address: { street: "987 North Street" }, hobbies: ["running"] })
- You can insert documents with nested objects and arrays:
Chapter 3: Basic Query Commands
Querying your data allows you to retrieve specific records. Here’s how:
-
Find All Records:
- To fetch all records from the users collection:
db.users.find()
- To fetch all records from the users collection:
-
Limit Results:
- To limit the number of results:
db.users.find().limit(2)
- To limit the number of results:
-
Sort Results:
- To sort by name in alphabetical order:
db.users.find().sort({ name: 1 })
- To sort by name in alphabetical order:
-
Complex Queries:
- To find users by specific criteria:
db.users.find({ name: "Kyle" })
- To find users by specific criteria:
Chapter 4: Complex Query Commands
For more complex queries, use the following techniques:
-
Use Comparison Operators:
- For age greater than a specific value:
db.users.find({ age: { $gt: 20 } })
- For age greater than a specific value:
-
Combine Queries:
- To find users with conditions:
db.users.find({ age: { $gte: 20, $lte: 40 } })
- To find users with conditions:
-
Logical Operators:
- To use logical conditions:
db.users.find({ $or: [{ age: { $lte: 20 } }, { name: "Kyle" }] })
- To use logical conditions:
Chapter 5: Update Commands
Updating records in MongoDB is essential for maintaining data integrity. Here’s how to do it:
-
Update a Single Record:
- To update a specific user’s age:
db.users.updateOne({ name: "Kyle" }, { $set: { age: 27 } })
- To update a specific user’s age:
-
Increment a Value:
- To increment a user’s age by 1:
db.users.updateOne({ name: "Kyle" }, { $inc: { age: 1 } })
- To increment a user’s age by 1:
-
Remove a Field:
- To unset a specific field:
db.users.updateOne({ name: "Kyle" }, { $unset: { age: "" } })
- To unset a specific field:
-
Update Multiple Records:
- To update all users with a specific condition:
db.users.updateMany({ address: { $exists: true } }, { $unset: { address: "" } })
- To update all users with a specific condition:
Chapter 6: Delete Commands
Deleting records is straightforward. Here’s how:
-
Delete a Single Record:
- To delete a specific user:
db.users.deleteOne({ name: "John" })
- To delete a specific user:
-
Delete Multiple Records:
- To delete all users without an age:
db.users.deleteMany({ age: { $exists: false } })
- To delete all users without an age:
Conclusion
In this tutorial, we've covered the essential commands and operations you need to work effectively with MongoDB. You now have the foundational skills to create, read, update, and delete records in a MongoDB database. For further learning, consider exploring advanced topics or practical applications of MongoDB in your projects. Happy coding!