MongoDB Guide
Database & Collection Creation, Document Operations & Queries
1. Install and Start MongoDB
mongod --dbpath="C:\data\db" # Windows
sudo systemctl start mongod # Linux/macOSmongosh # at cmd
Launch MongoDB Shell # at vsc 2. Create a Database
Switch or Create a Database
use myDatabase3. Create a Collection
Explicit Collection Creation
Implicit Collection Creation
4. Insert Documents
Insert a Single Document
Insert Multiple Documents
5. Find (Retrieve) Documents
Find All Documents
Pretty Print Results
Find One Document
6. Update Documents
Update a Single Document
Update Multiple Documents
7. Delete Documents
Delete One Document
Delete Multiple Documents
8. Sorting & Limiting Results
Sort in Ascending Order (1)
1)Sort in Descending Order (-1)
-1)Limit the Number of Results
9. Comparison Operators
Operator
Description
Example
10. Logical Operators
Operator
Description
Example
11. Indexes for Performance
Create an Index on a Field
View Existing Indexes
Delete an Index
12. Drop a Collection or Database
Drop a "users" Collection
Drop an Entire Database
Example Document & Data Types
Data Type
Example
Conclusion
Last updated