This website works best with JavaScript enabled.
Bookmark
🗄️ Database
show dbs                       # List databases
use mydb                       # Switch database
db                             # Current database
db.dropDatabase()              # Drop current database
📁 Collection
show collections               # List collections
db.createCollection("users")   # Create collection
db.users.drop()                # Drop collection
db.users.renameCollection("people") # Rename collection
📝 Document CRUD
db.users.insertOne({name: "Alice", age: 30})
db.users.insertMany([{name: "Bob"}, {name: "Carol"}])
db.users.find()                        # Find all
db.users.find({age: {$gt: 25}})        # Query with filter
db.users.findOne({name: "Alice"})
db.users.updateOne({name: "Alice"}, {$set: {age: 31}})
db.users.updateMany({}, {$inc: {age: 1}})
db.users.replaceOne({name: "Bob"}, {name: "Bobby", age: 28})
db.users.deleteOne({name: "Carol"})
db.users.deleteMany({age: {$lt: 20}})
🔍 Query & Projection
db.users.find({age: {$gte: 18}}, {name: 1, age: 1, _id: 0}) # Select fields
db.users.find().sort({age: -1})                              # Sort descending
db.users.find().limit(5)                                     # Limit results
db.users.find().skip(10)                                     # Skip results
db.users.countDocuments({})                                  # Count docs
🧩 Indexes
db.users.createIndex({name: 1})              # Create index
db.users.createIndex({age: -1, name: 1})     # Compound index
db.users.getIndexes()                        # List indexes
db.users.dropIndex("name_1")                 # Drop index
🛡️ Aggregation
db.users.aggregate([
  {$match: {age: {$gte: 18}}},
  {$group: {_id: "$age", count: {$sum: 1}}},
  {$sort: {count: -1}}
])
db.users.distinct("name")                    # Unique values
🔑 Admin & User
db.createUser({user: "admin", pwd: "pass", roles: ["userAdminAnyDatabase"]})
db.dropUser("admin")
db.changeUserPassword("admin", "newpass")
db.getUsers()
db.runCommand({connectionStatus: 1})
📦 Import & Export
mongoimport --db mydb --collection users --file users.json
mongoexport --db mydb --collection users --out users.json
💾 Backup & Restore
mongodump --db mydb --out ./backup
mongorestore --db mydb ./backup/mydb
🛠️ Misc
db.stats()                                  # Database stats
db.users.stats()                            # Collection stats
db.serverStatus()                           # Server info
Markdown
Nest

Want to update?

Subscribe to my blog to receive the latest updates from us.

Monthly articles
New articles will be created every month.
No spam
All notifications are notified to you, not spam or advertising.