Basic Commands
# Start MongoDB server
mongod
# Connect to MongoDB
mongo
Database Commands
# Show all databases
show dbs
# Switch to a database (or create it if it doesn't exist)
use database_name
# Get the current database
db
# Drop the current database
db.dropDatabase()
Collection Commands
# Show all collections in the current database
show collections
# Create a collection
db.createCollection("collection_name")
# Drop a collection
db.collection_name.drop()
Document Commands
# Insert a document into a collection
db.collection_name.insert({key1: "value1", key2: "value2"})
# Insert multiple documents into a collection
db.collection_name.insertMany([{key1: "value1"}, {key2: "value2"}])
# Find the first document that matches a query
db.collection_name.findOne({key: "value"})
# Find all documents in a collection
db.collection_name.find()
# Find documents that match a query
db.collection_name.find({key: "value"})
# Find documents with specific fields
db.collection_name.find({key: "value"}, {field1: 1, field2: 1})
# Update a document
db.collection_name.updateOne({key: "value"}, {$set: {field: "new_value"}})
# Update multiple documents
db.collection_name.updateMany({key: "value"}, {$set: {field: "new_value"}})
# Replace a document
db.collection_name.replaceOne({key: "value"}, {new_key: "new_value"})
# Delete a document
db.collection_name.deleteOne({key: "value"})
# Delete multiple documents
db.collection_name.deleteMany({key: "value"})
Query Operators
# Greater than
db.collection_name.find({key: {$gt: value}})
# Less than
db.collection_name.find({key: {$lt: value}})
# Greater than or equal to
db.collection_name.find({key: {$gte: value}})
# Less than or equal to
db.collection_name.find({key: {$lte: value}})
# Not equal to
db.collection_name.find({key: {$ne: value}})
# In
db.collection_name.find({key: {$in: [value1, value2]}})
# Not in
db.collection_name.find({key: {$nin: [value1, value2]}})
Index Commands
# Create an index
db.collection_name.createIndex({key: 1})
# Create a unique index
db.collection_name.createIndex({key: 1}, {unique: true})
# Drop an index
db.collection_name.dropIndex("index_name")
# Get all indexes
db.collection_name.getIndexes()
Aggregation Commands
# Count the number of documents in a collection
db.collection_name.countDocuments()
# Group documents by a key and perform an aggregation
db.collection_name.aggregate([
{$group: {_id: "$key", total: {$sum: "$field"}}}
])
# Match documents in an aggregation pipeline
db.collection_name.aggregate([
{$match: {key: "value"}}
])
# Project specific fields in an aggregation pipeline
db.collection_name.aggregate([
{$project: {field1: 1, field2: 1}}
])
Other Useful Commands
# Get information about the current connection
db.stats()
# Get information about a collection
db.collection_name.stats()
# Get the current operation
db.currentOp()
# Kill an operation
db.killOp(opid)
# Run a command
db.runCommand({command: 1})
# List all users
db.getUsers()
# Create a user
db.createUser({
user: "username",
pwd: "password",
roles: [{role: "readWrite", db: "database_name"}]
})
# Drop a user
db.dropUser("username")