1. MongoDB Basics
- MongoDB is a NoSQL, document-oriented database that stores data in flexible, JSON-like documents.
2. MongoDB Shell Commands
- Start MongoDB shell:
mongosh
- Show Databases:
show dbs
- Switch/Create Database:
use database_name
- Show Collections (tables equivalent):
show collections
3. Create a Collection
- Create Collection:
db.createCollection("collection_name")
Collections are created automatically when you insert a document.
4. CRUD Operations
Insert Documents
- Insert One Document:
db.collection_name.insertOne({key: "value", key2: "value2"})
- Insert Multiple Documents:
db.collection_name.insertMany([
{key: "value", key2: "value2"},
{key: "value3", key2: "value4"}
])
Find Documents
- Find All Documents:
db.collection_name.find()
- Find with Condition:
db.collection_name.find({key: "value"})
- Pretty Print:
db.collection_name.find().pretty()
Update Documents
- Update One Document:
db.collection_name.updateOne({key: "value"}, {$set: {key2: "new_value"}})
- Update Multiple Documents:
db.collection_name.updateMany({key: "value"}, {$set: {key2: "new_value"}})
Delete Documents
- Delete One Document:
db.collection_name.deleteOne({key: "value"})
- Delete Multiple Documents:
db.collection_name.deleteMany({key: "value"})
5. Querying Data
Operators in Queries
- Equality:
db.collection_name.find({key: "value"})
- Greater Than (GT):
db.collection_name.find({key: {$gt: value}})
- Less Than (LT):
db.collection_name.find({key: {$lt: value}})
- In:
db.collection_name.find({key: {$in: [value1, value2]}})
AND/OR Conditions
- AND Condition (default behavior):
db.collection_name.find({key1: "value1", key2: "value2"})
- OR Condition:
db.collection_name.find({$or: [{key1: "value1"}, {key2: "value2"}]})
6. Sorting, Limiting, and Pagination
- Sort Results:
db.collection_name.find().sort({key: 1}) // Ascending
db.collection_name.find().sort({key: -1}) // Descending
- Limit Results:
db.collection_name.find().limit(5)
- Skip Results (pagination):
db.collection_name.find().skip(5).limit(5)
7. Indexes
- Create Index:
db.collection_name.createIndex({key: 1})
1
for ascending, -1
for descending.
- Show Indexes:
db.collection_name.getIndexes()
8. Aggregation
- Basic Aggregation:
db.collection_name.aggregate([
{$match: {key: "value"}},
{$group: {_id: "$key2", total: {$sum: "$key3"}}}
])
9. Relationships (Embedding vs. Referencing)
- Embedding Documents (Store related data in the same document):
{
_id: 1,
name: "John",
address: {
street: "123 St",
city: "New York"
}
}
- Referencing Documents (Store references to related data):
{
_id: 1,
name: "John",
address_id: ObjectId("507f191e810c19729de860ea")
}
10. MongoDB Atlas
- MongoDB Atlas is a cloud database service for MongoDB.
Connecting to MongoDB Atlas
- Connection String (from MongoDB Atlas):
mongodb+srv://<username>:<password>@cluster0.mongodb.net/test
11. Backup and Restore
- Backup Database:
mongodump --db database_name
- Restore Database:
mongorestore --db database_name dump/database_name/
12. MongoDB Operators
- $set: Updates the value of a field.
- $inc: Increments the value of a field.
- $push: Appends a value to an array.
- $pull: Removes a value from an array.
- $exists: Checks for the existence of a field.
13. Other Useful Commands
- Count Documents:
db.collection_name.countDocuments()
- Drop Collection:
db.collection_name.drop()
- Drop Database:
db.dropDatabase()
14. Data Types
-
Common MongoDB Data Types:
- String
- Number (32-bit or 64-bit integer)
- Boolean
- Array
- Object
- ObjectId
- ISODate (for date/time)
15. Transactions (MongoDB 4.0+)
- Start Transaction:
session = db.getMongo().startSession();
session.startTransaction();
- Commit Transaction:
session.commitTransaction();
- Abort Transaction:
session.abortTransaction();
Top comments (0)