Sitemap

MongoDB | NoSQL database with 25 basic commands

3 min readApr 22, 2025

MongoDB is a powerful, flexible, and scalable NoSQL database that has revolutionized the way developers handle data. Unlike traditional relational databases, MongoDB uses a JSON like document-oriented approach, making it ideal for handling large volumes of unstructured data. Let’s dive into some key aspects of MongoDB and understand why it’s a popular choice for modern applications.

Key Features of MongoDB

  1. Document-Oriented Storage: MongoDB stores data in BSON (Binary JSON) format. This means you can store complex data structures and nested documents easily.
  2. Scalability: MongoDB has ability to scale horizontally. It can spread data across multiple servers to manage huge amounts of information without slowing down.
  3. Flexibility: MongoDB is schema-less, so you don’t need to define a strict structure for your data. You can add or remove fields as needed, making it perfect for projects where the data structure might evolve over time.
  4. High Performance: MongoDB provides high read and write throughput, making it suitable for applications that require real-time data processing.
  5. Rich Query Language: MongoDB offers a powerful query language that supports ad-hoc queries, indexing, and aggregation. You can perform complex searches and data manipulations with ease.

MongoDB counterparts to relational databases

Press enter or click to view image in full size
MongoDB counterparts to Relational databases

Must know tools in MongoDB ecosystem

  1. Atlas: A fully managed cloud database service that simplifies deploying, scaling, and managing MongoDB databases in the cloud.
  2. Mongosh: Shell environment which allows advanced features to communicate with MongoDB database.
  3. Compass: GUI interface that connect with MongoDB and performs advanced operations.

MongoDB architecture

1. Database: A container for collections in MongoDB.
2. Collection: A group of MongoDB documents.
3. Document: A JSON-like object containing key-value pairs.
4. Field: An individual key-value pair within a document.

Press enter or click to view image in full size

Top 25 commands in MongoDB

1. Shows all databases present

>show dbs

2. Show current database working on

>db

3. Switch database or create a new one

>use db

4. To drop current database

>db.dropDatabase()

5. To show all collections from current database

>show collections

6. Create a new collection

>db.createCollection("CollectionName")

7. To drop a collection

>db.collectionName.drop()

8. Insert a single document (one row) into a collection

>db.collectionName.insertOne({name:'Mahesh',lang:'Python'})

9. Insert more than one document (many rows) into a collection

>db.collectionName.insertMany([{name:'Mahesh',lang:'Python'},{name:'John',lang:'Java'}])

10. View all documents (rows) from a collection

>db.collectionName.find()

11. View filtered documents from collection based on field:values

>db.collectionName.find({language:'Python'})

12. To return only specific no. of documents

>db.collectionName.find().limit(3)

13. Sort documents based on a field (Ascending:1 and Descending:-1)

>db.collectionName.find().sort({name:1})  #sorts in ascending order
>db.collectionName.find().sort({name:-1}) #sorts in descending order

14. To view data in an understandable, easy to read, pretty format

>db.collectionName.find().pretty()

15. Get count of all documents

>db.collectionName.find().count()

16. Return only first row that matches the given condition

>db.collectionName.findOne({name:'mahesh'})

17. Update first document that matches the given condition

>db.collectionName.updateOne({name:"Mahesh"},{$set:{name:"Mahesh J",age:25}})

18. Update all documents that matches the given condition

>db.collectionName.updateMany({name:"Mahesh"},{$set:{name:"Mahesh J"}})

19. Update document that matches the given condition else create a new one

>db.collectionName.updateOne({name:"Mahesh"},{$set:{name:"Mahesh J"}},{upsert:true})

20. Increment the value of specified field for each matched document

>db.collectionName.updateOne({name:"Mahesh"},{$inc:{age:5}})
>db.collectionName.updateOne({name:"Mahesh"},{$inc:{age:-5}}) #decrease when given negative values

21. Rename field name for matched document

>db.collectionName.updateOne({name:"Mahesh"},{$rename:{language:"lang"}})

22. Remove all documents that match given condition

>db.collectionName..remove({name:"Mahesh"})

23. Delete a single document that matches given condition

>db.collectionName.deleteOne({age:26})

24. Delete all documents that matches given condition

>db.collectionName.deleteMany({dept:'sales'})

25. Using aggregate function to group, sort, perform calculations, analyze data, and much more.

>db.collectionName.aggregate([{$match:{age:{$gt:25}}}])  #returns documents that have age more than 25
>db.collectionName.aggregate([{$match:{age:{$gt:25}}},{$group:{_id:"$category",totallikes:{$sum:"$likes"}}}])

If you’re interested in learning more about MongoDB, check out the MongoDB Blog for the latest updates, tutorials, and best practices.

--

--

Mahesh Jadhav
Mahesh Jadhav

Written by Mahesh Jadhav

Part time developer, Full time debugger...

Responses (1)