Unit 2
Unit 2
Introduction to MongoDB
What is MongoDB?
Key Features:
Document-Oriented: MongoDB stores data in documents, which are similar to JSON objects.
Each document can have a different structure, making it easy to represent complex data.
Scalability: MongoDB is designed to scale horizontally, meaning you can distribute data
across multiple servers or clusters to handle large amounts of data and traffic.
Indexing: MongoDB supports the indexing of fields in documents, which improves query
performance. Indexes can be created on any field, including fields within arrays and
subdocuments.
Replication: MongoDB supports replica sets, which are groups of MongoDB servers that
maintain the same data set for high availability and fault tolerance.
Sharding: Sharding allows MongoDB to distribute data across multiple machines, enabling
horizontal scaling as data volume and throughput increase.
Basic Concepts:
Database: MongoDB can manage multiple databases, each of which can have its own
collections.
Document: A document is a set of key-value pairs and is the basic unit of data in MongoDB.
Documents are stored in BSON format.
Query Language:
MongoDB uses a flexible and expressive query language for reading and manipulating data.
The queries are expressed as JSON-like documents.
Use Cases:
Conclusion:
MongoDB's flexibility, scalability, and ease of use make it a popular choice for developers
working with large and diverse datasets. Whether you're building a small-scale application or
a large-scale system, MongoDB offers a range of features to meet your data storage and
retrieval needs.
show databases
Switch Database:
use your_database_name
Insert Document:
Query Documents:
db.your_collection_name.find()
Update Document:
Delete Document:
Advantages of MongoDB:
Flexible Schema: MongoDB's schema-less design allows you to evolve your data model over
time without downtime.
Scalability: Easily scales horizontally by sharding data across multiple servers or clusters.
High Performance: Provides high-speed reads and writes, and supports indexing for efficient
queries.
Horizontal Scaling: Scales out by adding more servers instead of scaling up a single server.
Rich Query Language: Supports a wide range of queries, including geospatial queries.