Home Blog MongoDB and its Basic Commands
MongoDB and its Basic Commands

MongoDB and its Basic Commands


MongoDB falling under the category of NoSQL databases is a widely used document-oriented database system. It is known to handle large datasets and thus provides a flexible and scalable solution to manage high-volume data efficiently. Data of any format will be stored in flexible documents, similar to JSON structures, called BSON (Binary JSON). MongoDB excels in handling both structured and unstructured data. This makes it ideal for modern, fast-paced applications.


In this article, we will dive into some of the most useful commands of MongoDB. Usi​ng these commands you can easily build your applications and manage the data effortlessly.


Basic Commands in MongoDB

MongoDB offers a variety of commands to interact with the database and perform data operations. Here are some essential commands to get you started:


Database and Collections

        1. Show dbs:

○ Shows all the databases present in the active state.

        2. use <first_database>:

○ This command is used to enter into the database.

○ Switch to the "first_database" database.

        3. show collections:

○ Lists all the collections in the current database.

     4. db.createCollection(“users”):

○ This creates the "users" collection.


Retrieving Data

        5. db.<collection_name>.find():

        Retrieves all documents from the specified collection.

○ db.users.find(): Retrieves all documents from the "users" collection.

○ db.users.find().pretty():  It works the same way as find() but it displays the documents in the prettier format.

○ db.users.find().pretty().count(): This will return the count of the number of documents.

○ db.users.find().pretty().limit(n): This will return the number of documents based on the “n” passed as an argument.

○    db.users.find().pretty().limit(3): This will return the first 3 documents from the “users” collection.


Inserting Data

        6. db.<collection_name>.insertOne(<document>):

○ Inserts a single document into the specified collection.

○ db.furniture.insertOne({"name": "Table", "price": 2000}):  Inserts a new document into the "furniture" collection.

     7. db.<collection_name>.insertMany(<documents>):

○  Inserts a single document into the specified collection.

db.furniture.insertMany({"name": "Table", "price": 2000},{“name”:”chair”,”price”:800},{...}):  Inserts new documents into the "furniture" collection.


Updating Documents

        8. db.<collection_name>.updateOne(<filter>, <update>):

Updates a single document in the specified collection based on the specified filter.

db.customers.updateOne({"name": "Test"}, {"$set": {"age": 20}}): Updates the "customers" collection, setting the age to 25 for the document with the name "John".  

Deleting Documents

        9. db.<collection_name>.deleteOne(<filter>):

Deletes a single document from the specified collection based on the specified filter.

db.orders.deleteOne({"status": "canceled"}): Deletes a document from the "orders" collection where the status is "canceled".

db.orders.deleteMany({}): Deletes multiple documents from the “orders” collection.

db.orders.deleteMany({price:800}): Deletes all the documents with price 800 in the “orders” collection.


 Subscribe our Newsletter for technical tips, insights, and more!


Conclusion

MongoDB provides a flexible and scalable solution for managing data. Manipulation of data becomes very easy for developers due to its straightforward approach and easy to remember commands. With all the basic commands mentioned in this article, you can confidently start working with MongoDB and build robust applications that handle large volumes of unstructured data.


Check out our other Blog on MongoDB: Connecting MongoDB with NodeJs



Get In Touch with Us

Leave a Comment

Your email address will not be published.

Submit
Your comment is under review by our moderation team.