I've been working with databases for years now, and I still remember how overwhelming it felt when I first started. That's why I wanted to put together this no-nonsense guide to MongoDB - one of my favorite database systems for modern web apps.
If you're just getting started with databases or looking to brush up on your MongoDB skills, you're in the right place. MongoDB has become super popular for good reason, and knowing how to work with it is practically a must-have skill for developers these days.
In this guide, I'll walk you through the commands I use daily - covering everything from setting up databases to manipulating your data. No fancy jargon, just practical stuff you can actually use. Let's jump right in!
Database Commands
Databases in MongoDB store collections, which contain documents (your actual data). Here are the essential commands to manage databases:
Creating a Database
When you need a new database, MongoDB makes it super easy. Just use the use
command, and MongoDB will create it on the fly if it doesn't exist:
use mydatabase
Viewing Databases
Need to see what databases you have? Just run:
show dbs
This shows you all your databases along with how much space they're taking up.
Switching Databases
Working on multiple projects? You can quickly jump between databases:
use dbName
Viewing Current Database
Sometimes I forget which database I'm in (happens to the best of us!). Just type:
db
Deleting a Database
Be careful with this one! When you're sure you want to delete a database:
db.dropDatabase()
I've accidentally deleted the wrong database more than once, so double-check before running this!
Collection Commands
Collections in MongoDB are similar to tables in relational databases. They store multiple documents.
Creating a Collection
Collections are like tables in traditional databases. Create one like this:
db.createCollection("users")
Viewing Collections
To see all collections in your current database:
show collections
Dropping a Collection
Need to delete a collection? Use:
db.users.drop()
Document Commands
Documents store actual data in MongoDB. These commands help you insert, retrieve, update, and delete documents.
Viewing Documents
To see all documents in a collection:
db.users.find()
View First Document
If you just need to check one document matching certain criteria:
db.comments.findOne({name: 'john'})
Prettified Output
When your data gets complex, readable formatting is a lifesaver:
db.users.find().pretty()
Inserting Documents
Add a single document to your collection:
db.users.insertOne({ name: "John", age: 30 })
Inserting Many Documents
Need to add multiple documents at once? Easy:
db.users.insertMany([
{ 'name': 'John', 'fruit': 'Cherry', 'age': 5 },
{ 'name': 'Alice', 'fruit': 'Banana', 'age': 3 },
{ 'name': 'Suzen', 'fruit': 'Mango', 'age': 4 }
])
Searching in MongoDB
Find exactly what you're looking for:
db.users.find({ age: '9' })
Limiting Output
When you only need a few results:
db.users.find().limit(2)
Counting Documents
Need to know how many documents match your query?
db.users.find().count()
Updating Documents
Change existing document data:
db.users.updateOne(
{ name: 'John' },
{ $set: { age: 35 } }
)
Deleting Documents
Remove documents when you don't need them anymore:
db.users.deleteOne({ name: "John" })
Comparison Operators
MongoDB's comparison operators make querying a breeze:
Less than:
db.users.find({ age: { $lt: 9 } })
Less than or equal to:
db.users.find({ age: { $lte: 9 } })
Greater than:
db.users.find({ age: { $gt: 9 } })
Greater than or equal to:
db.users.find({ age: { $gte: 9 } })
Additional Operations
MongoDB has tons of powerful operators. Here are a couple I use all the time:
Increment values:
db.users.update({ name: 'John' }, { $inc: { roll_no: 2 } })
Rename fields:
db.users.update({ name: 'John' }, { $rename: { roll_no: '10' } })
Conclusion
Understanding MongoDB commands is really important for managing databases well and building great apps. In this guide, we've learned lots of different commands. We've talked about making databases, managing collections, working with documents, and doing some cool stuff with them. By trying out these commands in your own projects, you'll get really good at using MongoDB and making awesome things. So, keep coding and have fun!
If you liked this blog, please share it with others who might find it useful. You can also keep up with me for more stuff about JavaScript, React, Next.js, MongoDB & Web Development.