MongoDB Cheat Sheet

MongoDB commands and queries reference

MongoDB Cheat Sheet

Quick reference guide for MongoDB commands and operations. Click the copy button to copy any command.

81
Total Commands
81
Filtered Results

Database Commands

CommandCopyDescription
show dbsList all databases
use <database>Switch to database
db.dropDatabase()Drop current database
db.getName()Get current database name
db.stats()Get database statistics

Collection Commands

CommandCopyDescription
show collectionsList all collections
db.createCollection("name")Create collection
db.collection.drop()Drop collection
db.collection.renameCollection("newName")Rename collection
db.collection.stats()Get collection statistics

Insert Operations

CommandCopyDescription
db.collection.insertOne({data})Insert single document
db.collection.insertMany([{data1}, {data2}])Insert multiple documents
db.collection.insert({data})Insert document (deprecated)

Find/Read Operations

CommandCopyDescription
db.collection.find()Find all documents
db.collection.find({key: value})Find with query
db.collection.findOne({query})Find single document
db.collection.find().limit(5)Limit results
db.collection.find().skip(10)Skip results
db.collection.find().sort({field: 1})Sort ascending
db.collection.find().sort({field: -1})Sort descending
db.collection.find().count()Count documents
db.collection.countDocuments({query})Count with query
db.collection.find({}, {field: 1})Projection (include field)
db.collection.find({}, {field: 0})Projection (exclude field)

Update Operations

CommandCopyDescription
db.collection.updateOne({query}, {$set: {data}})Update single document
db.collection.updateMany({query}, {$set: {data}})Update multiple documents
db.collection.replaceOne({query}, {data})Replace entire document
db.collection.update({query}, {$set: {data}})Update (deprecated)

Delete Operations

CommandCopyDescription
db.collection.deleteOne({query})Delete single document
db.collection.deleteMany({query})Delete multiple documents
db.collection.remove({query})Remove documents (deprecated)

Query Operators

CommandCopyDescription
{field: {$eq: value}}Equal to
{field: {$ne: value}}Not equal to
{field: {$gt: value}}Greater than
{field: {$gte: value}}Greater than or equal
{field: {$lt: value}}Less than
{field: {$lte: value}}Less than or equal
{field: {$in: [val1, val2]}}Value in array
{field: {$nin: [val1, val2]}}Value not in array
{$or: [{query1}, {query2}]}OR condition
{$and: [{query1}, {query2}]}AND condition
{$not: {query}}NOT condition
{field: {$exists: true}}Field exists
{field: {$type: "string"}}Field type
{field: {$regex: /pattern/}}Regex match

Update Operators

CommandCopyDescription
{$set: {field: value}}Set field value
{$unset: {field: ""}}Remove field
{$inc: {field: amount}}Increment field
{$mul: {field: multiplier}}Multiply field
{$rename: {oldField: "newField"}}Rename field
{$min: {field: value}}Update if value is smaller
{$max: {field: value}}Update if value is larger
{$currentDate: {field: true}}Set to current date

Array Operators

CommandCopyDescription
{$push: {array: value}}Add to array
{$pull: {array: value}}Remove from array
{$pop: {array: 1}}Remove last element
{$pop: {array: -1}}Remove first element
{$addToSet: {array: value}}Add unique value to array
{$each: [val1, val2]}Used with $push to add multiple
{array: {$all: [val1, val2]}}Array contains all values
{array: {$size: n}}Array size equals n

Aggregation

CommandCopyDescription
db.collection.aggregate([{$match: {query}}])Filter documents
db.collection.aggregate([{$group: {_id: "$field"}}])Group by field
db.collection.aggregate([{$sort: {field: 1}}])Sort in pipeline
db.collection.aggregate([{$limit: 10}])Limit in pipeline
db.collection.aggregate([{$skip: 5}])Skip in pipeline
db.collection.aggregate([{$project: {field: 1}}])Project fields
db.collection.aggregate([{$lookup: {from: "other"}}])Join collections
db.collection.aggregate([{$unwind: "$array"}])Deconstruct array

Indexes

CommandCopyDescription
db.collection.createIndex({field: 1})Create ascending index
db.collection.createIndex({field: -1})Create descending index
db.collection.createIndex({field: 1}, {unique: true})Create unique index
db.collection.getIndexes()List all indexes
db.collection.dropIndex("indexName")Drop specific index
db.collection.dropIndexes()Drop all indexes

Text Search

CommandCopyDescription
db.collection.createIndex({field: "text"})Create text index
db.collection.find({$text: {$search: "query"}})Text search

Bulk Operations

CommandCopyDescription
db.collection.bulkWrite([operations])Bulk write operations

Data Types

CommandCopyDescription
ObjectId()Generate new ObjectId
ISODate()Current date/time
ISODate("2024-01-01")Specific date

All operations are performed locally in your browser. No data is sent to any server.

About MongoDB Cheat Sheet

This section will contain detailed, SEO-friendly content about the MongoDB Cheat Sheet.

In the future, this content will be managed through a headless CMS, allowing you to:

  • Add detailed explanations about how to use this tool
  • Include examples and use cases
  • Provide tips and best practices
  • Add FAQs and troubleshooting guides
  • Update content without touching the code

How to Use

Step-by-step instructions for using the MongoDB Cheat Sheet will appear here. This content will be fully customizable through the admin panel.

Features

Key features and benefits of this tool will be listed here. All content is editable via the CMS.