MongoDB Cheatsheet



The MongoDB cheatsheet provides a quick reference to all the fundamental topics. Web applications commonly use MongoDB, a NoSQL database management system, to store and manage large amounts of complex data. By learning this cheat sheet, one can prepare for the interviews and exams. Go through the cheat sheet and learn the MongoDB database.

1. MongoDB Introduction

MongoDB is a document-based database program that stores the data in the form of JSON documents. It is also known as NoSQL database which means non-tabular and different from the relational database.

2. Installation of MongoDB

To install the MongoDB to your system, use the following steps −

Step 1: Go to the community server Download and dowload the Here, we mentioned the link −

  • https://www.mongodb.com/try/download/community

Step 2: Start by opening "Edit the system environment variable" and set the path from your local system.

  • C:\Program Files\MongoDB\Server\8.0\bin

Step 3: Download the MongoDB shell from the browser.

Step 4: After downloading, check the following command in the command prompt for successful installation.

< mongod --version

3. Create a Database

The use command is used to create or switch in a database.

use Database_name

4. Create a Collection

By using db.createCollection() method, the user can create a collection.

db.createCollection("myCollection")

5. Drop a Database

The use of db.dropDatabase() to delete the current database.

db.dropDatabase()

6. Insert Document

To insert document, use the method insertOne() or insertMany() to add documents.

db.myCollection.insertOne({ name: "Ravi", age: 22, city: "Bhagalpur" })

7. Display Document

The find() is used to retrieve all the documents.

db.myCollection.find()

8. pretty() Method

The pretty() is used to format the output.

db.myCollection.find().pretty()

9. Show Collection

The show collection defines the list of all collections in the database.

show collections

10. AND Operator

The $and operator is used to filter the documents.

db.myCollection.find({ $and: [{ age: { $gt: 20 } }, { city: "New York" }] })

11. OR Operator

The $or operator is used to perform the logical OR operation in queries. It allows the user to specify multiple conditions, and the query will return at least one of the specified columns./p>

db.myCollection.find({ $or: [{ age: 25 }, { city: "Los Angeles" }] })

12. NOR Operator

The $nor operator in MongoDB is used to perform a logical NOR operation. It returns documents that do not match any of the conditions specified.

db.myCollection.find({ $nor: [{ age: 25 }, { city: "New York" }] })

13. NOT Operator

The use of the $not operator is to exclude specific values.

db.myCollection.find({ age: { $not: { $gt: 25 } } })

14. LIMIT Document

The use of limit() is to return a specific number of documents.

db.myCollection.find().limit(5)

15. SKIP Documents

The SKIP Document defines the skip() to skip/leave a certain number of documents.

db.myCollection.find().skip(3)

16. Insert a Single Document

In MongoDB, the insertone() method is used to insert a single document into a collection.

db.myCollection.insertOne({ name: "Mark", age: 37, city: "Tokyo" })

Explanation:

  • myCollection: This is the name of the collection.
  • { name: "Mark", age: 37, city: "Tokyo" }: This is document which is in the form of key−value pairs.

17. Sort Record

In MongoDB, the sort record is used to arrange the fields in ascending or descending order.

// Ascending order
db.myCollection.find().sort({ field: 1 })

// Descending order  
db.myCollection.find().sort({ field: -1 }) 

18. Text Index

In MongoDB, a text index is used to perform text searches on string fields within documents.

db.myCollection.createIndex({ name: "text" })

19. LIMIT with SKIP in a Query

The skip() and limit() combine to perform in a single line of query, which controls the number of documents returned in a query result.

db.myCollection.find().skip(2).limit(5)

20. find() Method

In MongoDB, the find() method is used to retrieve a document from a collection.

db.myCollection.find({ age: { $gt: 25 } })

In MongoDB, text search plays a powerful role in searching large volumes of textual data. It allows the user to search for words or phrases within the string field.

db.myCollection.find({ $text: { $search: "Aman" } })

22. Insert Multiple Documents

To insert the multiple documents at once, use the method insertMany().

db.myCollection.insertMany([
    { name: "Ravi", age: 22 },
    { name: "Pinki", age: 28 }
])

Note: For inserting the row one by one, use the method insertone().

23. explain()

In MongoDB, explain() method is use get the detail information about query execution in the databases.

db.myCollection.find({ age: 25 }).explain("executionStats")

24. aggregate()

The aggregate() method in MongoDB allows users to process data and return computed results.

db.myCollection.aggregate([
    { $match: { age: { $gt: 20 } } },
    { $group: { _id: "$city", total: { $sum: 1 } } }
])

25. Create Text Index

In MongoDB, create a text index and specify the field of the index with the type "text".

db.myCollection.createIndex({ description: "text" })

26. Delete Text Index

The delete text index removes the previous text index from the collection.

db.myCollection.dropIndex("description_text")

27. Stats

In MongoDB, stats() is used to access statistics about a collection. For example, size, number of documents, storage size, and indexes.

db.stats()

28. listCommands()

In MongoDB, the listCommands() method is used to fetch a list of all available database commands.

db.runCommand({ listCommands: 1 })

29. getCollectionInfos()

In MongoDB, the getCollectionInfos() method retrieves metadata about collections in a database.

db.getCollectionInfos()
Advertisements