Friday, 7 April 2023

Getting Started with MongoDB: Basic Commands for Beginners

 MongoDB is a popular NoSQL database system that stores data in a JSON-like format called BSON. It is used by many companies and organizations around the world to manage their data. Here are some basic MongoDB commands that are useful for beginners:



Show databases: This command displays a list of all the databases currently present in the MongoDB server.

show databases


Use database: This command is used to switch to a particular database. You can use this command to access the collections in the specified database.

use myDatabase


Show collections: This command displays a list of all the collections present in the current database.

show collections


Insert: This command is used to insert a document into a collection. A document is a basic unit of data in MongoDB, similar to a row in a table in a relational database.

db.myCollection.insert({ "name": "John", "age": 30 })


Find: This command is used to retrieve documents from a collection. You can use this command to query the collection based on specific criteria.

db.myCollection.find({ "name": "John" })


Update: This command is used to update a document in a collection. You can use this command to modify one or more fields in a document.

db.myCollection.update({ "name": "John" }, { $set: { "age": 35 } })


Delete: This command is used to delete a document from a collection.

db.myCollection.deleteOne({ "name": "John" })


Aggregate: This command is used to perform aggregate operations on a collection, such as grouping and counting documents.

db.myCollection.aggregate([{ $group: { _id: "$name", count: { $sum: 1 } } }])


These are just some of the basic commands in MongoDB. As you become more familiar with the system, you'll likely find more advanced commands that can help you manage your data even more effectively.


No comments:

Post a Comment