MongoDB Database Cheatsheet

MongoDB is a popular NoSQL database that provides a flexible and scalable solution for managing large amounts of data. Whether you’re a beginner or an experienced developer, having a cheatsheet handy can save time and help you navigate MongoDB with ease. In this blog, we’ll cover essential MongoDB commands and concepts organized into a cheatsheet format.

Connecting to MongoDB

Connection String

mongo "mongodb+srv://<username>:<password>@cluster.mongodb.net/<database>"

Connect to Local Instance

mongo

Basic Operations

Create Database

use mydatabase

Show Databases

show dbs

Create Collection

db.createCollection("mycollection")

Show Collections

show collections

CRUD Operations

Insert Document

db.mycollection.insertOne({ key: "value" })

Find Documents

db.mycollection.find()

Update Document

db.mycollection.updateOne({ key: "value" }, { $set: { key: "newvalue" } })

Delete Document

db.mycollection.deleteOne({ key: "value" })

Querying

Basic Query

db.mycollection.find({ key: "value" })

Projection

db.mycollection.find({ key: "value" }, { key: 1, _id: 0 })

Sorting

db.mycollection.find().sort({ key: 1 })

Indexing

Create Index

db.mycollection.createIndex({ key: 1 })

List Indexes

db.mycollection.getIndexes()

Drop Index

db.mycollection.dropIndex("index_name")

Aggregation

Grouping

db.mycollection.aggregate([
  { $group: { _id: "$key", count: { $sum: 1 } } }
])

Projecting

db.mycollection.aggregate([
  { $project: { key: 1, _id: 0 } }
])

Backup and Restore

Export Data

mongodump --uri="mongodb+srv://<username>:<password>@cluster.mongodb.net/<database>" --out /path/to/dump

Import Data

mongorestore --uri="mongodb+srv://<username>:<password>@cluster.mongodb.net/<database>" /path/to/dump

This MongoDB cheatsheet provides a quick reference for essential commands and operations. Whether you’re inserting documents, querying data, or performing advanced aggregations, having these commands at your fingertips can significantly boost your productivity. Keep this cheatsheet handy for a seamless MongoDB experience.

FAQ

1. What is MongoDB and why use it?

MongoDB is a NoSQL database that stores data in flexible, JSON-like documents. It is designed for scalability, performance, and ease of development, making it an ideal choice for applications with evolving data requirements.

2. How do I connect to a MongoDB database?

To connect to MongoDB, you can use a connection string specifying the database server, username, password, and database name. Alternatively, for a local instance, the mongo command in the terminal can be used.

3. What is the difference between findOne and find in MongoDB?

The findOne method returns the first document that matches the query criteria, while the find method returns a cursor to the documents that match the query. To retrieve all matching documents, you can iterate over the cursor or use methods like toArray().

4. How can I create an index in MongoDB?

You can create an index in MongoDB using the createIndex method. Specify the field(s) on which to create an index and the desired order (ascending or descending). Indexing can significantly improve query performance.

5. What is the purpose of MongoDB’s Aggregation framework?

MongoDB’s Aggregation framework is used for data transformation and analysis. It provides powerful tools for grouping, filtering, and projecting data, making it useful for tasks like calculating aggregates, sorting, and shaping data before it is presented to an application.