MongoDB cheat sheet

Read Time:3 Minute, 20 Second

Here is a cheat sheet for mongodb

Basic Idea

Database: A container of collection.
Collection: Grouping of documents insida of a database. Similar tables in SQL.
Document: A record inside of a collection. Similar to row in SQL.
Field: A key value pair within a document. Similar to column in SQL.

Basic commands

mongosh: A JavaScript shell for interacting with MongoDB instances. It provides a command-line interface (CLI) that allows you to connect to a MongoDB server.
show dbs: Shows all databases in the current MongoDB instance.
use <dbname>: Switch database provided by dbname.
db: Shows current database name.
show collections: Shows all collections.
db.dropDatabase(): Deletes the current database.
exit: Exits the mongosh session.

Create

insertOne: Creates a document within the specified collection.

Enter fullscreen mode Exit fullscreen mode

insertMany: Creates multiple documents within the specified collection.

Enter fullscreen mode Exit fullscreen mode

Read

find: Get all documents.

Enter fullscreen mode Exit fullscreen mode

find(<filterObject>): Find all documents based on the filter object

Enter fullscreen mode Exit fullscreen mode

find(<filterObject>, <selectObject>): Find all documents that match the filter object but only return the field specified in the select object

Enter fullscreen mode Exit fullscreen mode

findOne: Returns the first document that matches the filter object.

Enter fullscreen mode Exit fullscreen mode

countDocuments: Returns the count of the documents that match the filter object.

Enter fullscreen mode Exit fullscreen mode

Update

updateOne: Updates the first document.

Enter fullscreen mode Exit fullscreen mode

updateMany: Updates multiple docments.

Enter fullscreen mode Exit fullscreen mode

replaceOne: Replace the first document. This
will completely overwrite the entire object and not just
update individual fields.

Enter fullscreen mode Exit fullscreen mode

Delete

deleteOne: Delete a single document from a collection.

Enter fullscreen mode Exit fullscreen mode

deleteMany: Delete multiple documents from a collection.

Enter fullscreen mode Exit fullscreen mode

Complex Filter Object

$eq: equals.

Enter fullscreen mode Exit fullscreen mode

$ne: not equal to.

Enter fullscreen mode Exit fullscreen mode

$gt / $gte: Greater than and greater than or eqal.

Enter fullscreen mode Exit fullscreen mode

$lt / $lte: Less than and less than or eqal.

Enter fullscreen mode Exit fullscreen mode

$in: Check if a value is one of many values.

Enter fullscreen mode Exit fullscreen mode

$nin: Check if a value is none of many values.

Enter fullscreen mode Exit fullscreen mode

$and: Returns true if all expressions are true

Enter fullscreen mode Exit fullscreen mode

$or: returns true if any expression is true

Enter fullscreen mode Exit fullscreen mode

$not: Negates the expression

Enter fullscreen mode Exit fullscreen mode

$exists: Matches documents that have the specified field.

Enter fullscreen mode Exit fullscreen mode

$expr: performs an expression evaluation in the query.

Enter fullscreen mode Exit fullscreen mode

Complex Update Object

$set: Updates only the fields passed to $set.

Enter fullscreen mode Exit fullscreen mode

$inc: Increments the value of a field by a specified amount.

Enter fullscreen mode Exit fullscreen mode

$rename: Rename a field

Enter fullscreen mode Exit fullscreen mode

$unset: Remove a field.

Enter fullscreen mode Exit fullscreen mode

$push: Adds new elements to an array

Enter fullscreen mode Exit fullscreen mode

$pull: Rmoves all array elements that match a specified condition.

Enter fullscreen mode Exit fullscreen mode

Modifiers for read

sort: Sort the results of a find by the given fields.

Enter fullscreen mode Exit fullscreen mode

limit: Returns a specified number of documents.

Enter fullscreen mode Exit fullscreen mode

skip: Skip a specified number of documents from the start.

Enter fullscreen mode Exit fullscreen mode

Source: https://dev.to/arafat4693/mongodb-cheat-sheet-2f9o

Tag Cloud

Java Java Logical Programs OTP Generation in Java python Recursion youtube video ASCII Upper and Lower Case blockchain javascript graph learn to code software development Successful Software Engineers breadth first search Java Array Programs Java Programs Uncategorized android ios programming kotlin web-development django data sql cybersecurity database swiftui serverless aws swift rust react background-position gradients loader mask grid nth-child pseudo elements indieweb WordPress Print Array without brackets C++ factorial Java String Programs Final Keyword Static Variable Axie Infinity Cryptokitties NFT games tool inserting MISC Tips Codes python code python projects python3 system info python project Bigginers How to Do Integrations Payment Gateways PHP checkout page in php Implement stripe payment gateway in Step by step in PHP integrate stripe gatway in php mysql payment gateway integration in php step by step payment gateway integration in php step by step with source code payment gateway integration in website PHP Integrate Stripe Payment Gateway Tutorial PHP shopping cart checkout code shopping cart in php stripe php checkout PHP/MySQL/JSON best international payment gateway does google pay accept international payments how to accept international payments in india paytm payment gateway razorpay codeigniter github razorpay custom checkout github razorpay get payment details razorpay integration in codeigniter github razorpay international payments Razorpay payment gateway integration in CodeIgniter razorpay payment gateway integration in php code Razorpay payment gateway integration with PHP and CodeIgniter Razorpay payment gateway setup in CodeIgniter Library & Frameworks Tips & Tricks UI/UX & Front-end coding birds online html code for google sign in login with google account in PHP login with google account using javascript login with google account using javascript codeigniter login with google account using php login with google account using php source code
How to add elements into an array in JavaScript Previous post How to add elements into an array in JavaScript
7 free Tools for the Modern Web Developers of 2023 Next post 7 free Tools for the Modern Web Developers of 2023

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.