0% found this document useful (0 votes)
6 views3 pages

MongoDB Cheat Sheet (2)

This MongoDB cheat sheet provides essential commands for database management, including basic commands, CRUD operations, collection operations, indexing, and aggregation examples. It covers how to create, read, update, and delete documents, as well as manage collections and indexes. Additionally, it includes commands for retrieving database and collection statistics.

Uploaded by

hari prasanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

MongoDB Cheat Sheet (2)

This MongoDB cheat sheet provides essential commands for database management, including basic commands, CRUD operations, collection operations, indexing, and aggregation examples. It covers how to create, read, update, and delete documents, as well as manage collections and indexes. Additionally, it includes commands for retrieving database and collection statistics.

Uploaded by

hari prasanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

MongoDB Cheat Sheet

1. Basic Commands

-----------------

show dbs // List all databases

use myDatabase // Switch to (or create) a database

db // Show current database

show collections // List collections in current DB

2. CRUD Operations

------------------

Create / Insert

---------------

db.users.insertOne({ name: "Alice", age: 25 }) // Insert one document

db.users.insertMany([{ name: "Bob" }, { name: "Eve" }]) // Insert multiple

Read / Query

------------

db.users.find() // Find all documents

db.users.findOne({ name: "Alice" }) // Find one

db.users.find({ age: { $gt: 20 } }) // Find with condition

db.users.find().pretty() // Prettify output

Update

------
db.users.updateOne(

{ name: "Alice" },

{ $set: { age: 26 } }

db.users.updateMany(

{ age: { $lt: 18 } },

{ $set: { status: "minor" } }

Delete

------

db.users.deleteOne({ name: "Alice" }) // Delete one

db.users.deleteMany({ age: { $lt: 18 } }) // Delete many

3. Collection Operations

------------------------

db.createCollection("products") // Create collection

db.products.drop() // Delete collection

4. Indexing

-----------

db.users.createIndex({ name: 1 }) // Create ascending index

db.users.getIndexes() // List indexes

5. Aggregation Example
----------------------

db.orders.aggregate([

{ $match: { status: "delivered" } },

{ $group: { _id: "$customerId", total: { $sum: "$amount" } } }

])

6. Miscellaneous

----------------

db.stats() // DB statistics

db.users.stats() // Collection stats

db.users.countDocuments() // Count documents

You might also like