MongoDB Cheat Sheet
MongoDB user?
Then, bookmark this post for later. 🔖
It's a cheat sheet of all the basics of MongoDB:
Key Concepts
What is MongoDB?
MongoDB is a document-based database for flexibility, scalability, and rich querying. It stores data in BSON format, a binary representation of JSON documents, providing additional data types over JSON.
Database Structure
- Database: A database is a logical grouping of collections. Each database has its own set of files on the file system, usually associated with a single application.
- Collection: A collection is a group of MongoDB documents analogous to a table in relational databases but without a predefined schema.
- Document: A BSON document representing a data record. Each document has a dynamic schema of field and value pairs. Documents can contain nested documents, arrays, and arrays of documents.
- Field: A key-value pair in a document. Fields in documents in the same collection can vary, and the data type of a field's value can be any BSON data type, including another document, an array, or an array of documents.
- Primary Key: The unique
_id
field in each document. If not specified, MongoDB generates anObjectId
for it automatically.
Advantages of MongoDB
- Flexibility: Dynamic schemas allow for documents in the same collection to have different fields.
- Scalability: Easily scales horizontally with sharding.
- Performance: Offers high performance for read and write operations and supports indexes for efficient queries.
- Rich Query Language: Supports a powerful query language to filter and manipulate data.
MongoDB Shell Commands 🍃🌲
Basic Operations
- Show Databases:
show dbs
- Use Database:
use <database_name>
- Show Collections:
show collections
- Create Collection:
db.createCollection("<collection_name>")
- Drop Database:
db.dropDatabase()
- Drop Collection:
db.<collection_name>.drop()
Document Operations
- Insert a Document:
db.<collection_name>.insertOne({<key>: <value>})
- Insert Multiple Documents:
db.<collection_name>.insertMany([{<key>: <value>}, {...}])
- Find Documents:
db.<collection_name>.find()
- Find Documents with Query:
db.<collection_name>.find({<key>: <value>})
- Find One Document:
db.<collection_name>.findOne({<key>: <value>})
- Update a Document:
db.<collection_name>.updateOne({<filter>}, {$set: {<key>: <value>}})
- Delete a Document:
db.<collection_name>.deleteOne({<key>: <value>})
Advanced Queries and Operations
- Count Documents:
db.<collection_name>.countDocuments({<query>})
- Limit Documents:
db.<collection_name>.find().limit(<number>)
- Sort Documents:
db.<collection_name>.find().sort({<key>: 1}) // 1 for ascending, -1 for descending
- Aggregation Framework:
db.<collection_name>.aggregate([{$group: {_id: "$<field>", total: {$sum: 1}}}])
Query Modifiers
- Comparison Operators:
$gt
,$gte
,$lt
,$lte
,$ne
for greater than, greater than or equal, less than, less than or equal, and not equal conditions. - Logical Operators:
$and
,$or
to combine multiple query conditions. - Element Operators:
$exists
,$type
to query documents based on the presence of a field or the type of its value.
Indexes
- Create Index:
db.<collection_name>.createIndex({<key>: 1}) // 1 for ascending, -1 for descending
Additional Tips
- MongoDB supports ACID transactions for multi-document operations, enhancing application consistency.
- Use MongoDB Atlas for a managed database service with built-in best practices for security, backup, and monitoring.