Definition:
MongoDB is a cross-platform, document-oriented database program that falls under the category of NoSQL databases. It is designed to store, retrieve, and manage data in a flexible, schema-free format called BSON (Binary JSON), which is a binary-encoded serialization of JSON-like documents.
Key features of MongoDB include:
1. Document-Oriented: MongoDB stores data in flexible, JSON-like documents called BSON documents. Each document can have a different structure, allowing for varied and dynamic data.
2. No Schema Constraints: Unlike traditional relational databases, MongoDB does not enforce a rigid schema for the data. This flexibility is particularly useful for applications with evolving and dynamic data models.
3. Scalability: MongoDB is designed to scale horizontally by distributing data across multiple servers. This makes it well-suited for handling large amounts of data and high traffic loads.
4. Indexing: MongoDB supports indexing to improve query performance. Indexes can be created on any field within a document, making it faster to search and retrieve data.
5. Query Language: MongoDB uses a rich and expressive query language, which includes support for complex queries, aggregations, and geospatial queries.
6. High Availability: MongoDB provides features like replication and sharding to ensure high availability and fault tolerance. Replication allows data to be mirrored across multiple servers, while sharding distributes data across multiple machines.
7. Ad Hoc Queries: MongoDB supports ad hoc queries, meaning you can perform powerful and dynamic queries on your data without the need for predefined views or stored procedures.
8. Open Source: MongoDB is open-source software, and there is a vibrant community around it. It is maintained by MongoDB Inc., and there are both community and enterprise versions available.
Overall, MongoDB is well-suited for applications with large and evolving datasets, where flexibility and scalability are essential. It has gained popularity in web development, mobile app development, and other scenarios where a flexible and scalable database solution is needed.
MongoDB Installation Steps:
1. Visit the Official MongoDB Compass Download Page:
Open your web browser and go to the official MongoDB Compass download page: MongoDB Compass Download (GUI)
2. Select Your Operating System:
Choose your operating system from the options provided (Windows, macOS, or Linux).
For Windows:
Select the version suitable for your Windows system (32-bit or 64-bit).
Click on the Download button.
For macOS:
Select the macOS version.
Click on the Download button.
For Linux:
Choose the Linux distribution you are using.
Follow the instructions provided for your specific distribution.
3. Download MongoDB Compass:
Once selected, click on the Download button to initiate the download.
4. Run the Installer:
For Windows:
Double-click the downloaded .msi file.
Follow the on-screen instructions to install MongoDB Compass.
For macOS:
Double-click the downloaded .dmg file.
Drag the MongoDB Compass icon to the Applications folder.
For Linux:
Follow the instructions for your Linux distribution. Typically, this involves using package management tools like apt or yum.
5. Start MongoDB Compass:
For Windows:
MongoDB Compass should be accessible from the Start menu or desktop shortcuts.
For macOS:
Open the Applications folder and find MongoDB Compass. Double-click to launch it.
For Linux:
Start MongoDB Compass using the appropriate command or through the graphical interface, depending on your distribution.
6. Connect to MongoDB Server:
Open MongoDB Compass.
Click on “Connect” to configure and connect to your MongoDB server.
Enter the necessary connection details such as hostname, port, and authentication settings.
Once connected, you can explore your MongoDB databases, collections, and documents using the graphical interface provided by MongoDB
Compass.
MongoDB queries for CRUD operations (Create, Read, Update, Delete):
Create (Insert) Documents:
Insert a Single Document:
db.collectionName.insertOne({ field1: value1, … }); |
Insert Multiple Documents:
db.collectionName.insertMany([
{ field1: value1, field2: value2, … },
{ field1: value3, field2: value4, … },
//Add more documents as needed
]); |
Read (Query) Documents:
Find All Documents:
db.collectionName.find(); |
Find Documents with a Specific Condition:
db.collectionName.find({ field: value }); |
Limit the Number of Results:
db.collectionName.find().limit(5); |
Projection (Select Specific Fields):
db.collectionName.find({}, { field1: 1,field2: 1, _id: 0 }); |
Update Documents:
Update a Single Document:
db.collectionName.updateOne({ field: value }, { $set: { updatedField: newValue } }); |
Update Multiple Documents:
db.collectionName.updateMany({ field: value }, { $set: { updatedField: newValue } }); |
Replace a Document:
db.collectionName.replaceOne({ field: value }, { field1: newValue1, field2: newValue2, … }); |
Delete Documents:
Delete a Single Document:
db.collectionName.deleteOne({ field: value }); |
Delete Multiple Documents:
db.collectionName.deleteMany({ field: value }); |
Delete All Documents in a Collection:
db.collectionName.deleteMany({}); |