MongoDB is a powerful, flexible database solution that allows for scalable data storage, making it popular for applications requiring a robust NoSQL database. This cheat sheet compiles key commands, structures, and examples to streamline your workflow.
To begin using MongoDB, follow these steps for installation:
brew tap mongodb/brew brew install mongodb-community
sudo apt update sudo apt install -y mongodb
To start your MongoDB server, use the following command:
mongod
This launches your MongoDB server process. You can access the database from another terminal with:
mongo
To view your current databases:
show dbs
To create a new database, you can use the use
command:
use myDatabase
This command switches to myDatabase
, creating it if it doesn’t exist.
To delete a database permanently:
db.dropDatabase()
Use the following command to create a collection:
db.createCollection('myCollection')
To remove an existing collection:
db.myCollection.drop()
To see all collections in your current database:
show collections
To insert a single document into a collection:
db.myCollection.insertOne({ name: "John Doe", age: 30 })
To insert multiple records at once:
db.myCollection.insertMany([ { name: "Jane Doe", age: 25 }, { name: "Mike Smith", age: 35 } ])
To retrieve a single document:
db.myCollection.findOne({ name: "John Doe" })
To retrieve all documents in a collection:
db.myCollection.find()
You can filter results using conditions:
db.myCollection.find({ age: { $gte: 30 } })
This will find all documents where age is greater than or equal to 30.
To update a single document:
db.myCollection.updateOne( { name: "John Doe" }, { $set: { age: 31 } } )
To update multiple documents matching a condition:
db.myCollection.updateMany( { age: { $lt: 30 } }, { $set: { status: "young" } } )
To delete a single document:
db.myCollection.deleteOne({ name: "John Doe" })
To delete multiple documents:
db.myCollection.deleteMany({ age: { $lt: 30 } })
Indexes improve query performance. To create one, use:
db.myCollection.createIndex({ name: 1 })
This creates an ascending index on the name
field.
To view all indexes on a collection:
db.myCollection.getIndexes()
The aggregation framework allows you to perform complex data processing tasks.
To combine data from documents:
db.myCollection.aggregate([ { $match: { age: { $gte: 30 } } }, { $group: { _id: "$name", averageAge: { $avg: "$age" } } } ])
To create a backup of your database:
mongodump --db myDatabase --out /path/to/backup
To restore your database from a backup:
mongorestore /path/to/backup
To add a user with read and write permissions:
db.createUser({ user: "myUser", pwd: "myPassword", roles: [{ role: "readWrite", db: "myDatabase" }] })
To require users to log in, start the server with:
mongod --auth
With this cheat sheet, you can quickly refer to essential MongoDB commands and concepts while developing your applications. Keep this handy as you navigate the powerful features that MongoDB offers!
09/11/2024 | MongoDB
09/11/2024 | MongoDB
09/11/2024 | MongoDB
09/11/2024 | MongoDB
09/11/2024 | MongoDB
09/11/2024 | MongoDB
09/11/2024 | MongoDB
09/11/2024 | MongoDB