This document provides information about MongoDB, including:
- MongoDB is a non-SQL database that stores data as flexible documents rather than rows and tables. It is suitable for large, unstructured datasets.
- Key features include document-oriented storage, full indexing support, replication for high availability, auto-sharding for scalability, and querying capabilities.
- CRUD operations like insert, find, update, and delete can be performed on MongoDB collections and documents using methods like db.collection.insert() and db.collection.find(). Aggregation operations allow computing results by processing documents.
2. Why MongoDB?
• MongoDB is a NoSQL (Not only SQL) database that stores large volumes of data
in the form of documents.
• MongoDB removes the concept of "rows" of conventional and relational data
models by introducing “documents”.
• MongoDB is more flexible and ensures high and diverse data availability.
3. When to use MongoDB?
• You have lots of unstructured data. MongoDB (and NoSQL databases in general)
has no storable data type limits.
• You have schema issues. If you have an unstable or undefined schema, use
MongoDB.
4. Features of MongoDB
• Document-Oriented storage
• Full Index Support
• Replication & High Availability
• Auto-Sharding
• Querying
Scalable
5. Document-oriented storage:
• MongoDB is a document-oriented NoSQL database. It is used for storing
massive volumes of data. Unlike a traditional SQL relational database,
MongoDB does not rely on tables and columns. Data is stored as collections
and documents.
• Data is stored in BSON format internally and over the network but retrieved
as JSON.
6. • Index is a way to organize information so that the database engine can quickly
find the relevant results.
• Indexes in MongoDB are similar to indexes in other database systems. MongoDB
defines indexes at the collection level and supports indexes on any field or sub-
field of the documents in a MongoDB collection.
Full-index support:
7. • Replication is performed in MongoDB to maintain data redundancy and high
availability. It is performed by creating multiple copies of your data across
multiple servers.
Replication and high availability:
8. • Sharding is a method for distributing or partitioning data across multiple
machines.
• It is useful when no single machine can handle large modern-day workloads, by
allowing you to scale horizontally.
Auto-Sharding:
9. Querying:
• MongoDB Query is a way to get the data from the MongoDB database.
• MongoDB queries provide the simplicity in process of fetching data from the
database.
• MongoDB provides the function names as db.collection_name.find() to operate
query operation on database.
• Database: Newspaper
• Collection: Article
• Query: db.article.find() // returns all the documents in the Article collection
15. Aggregation Functions
• Aggregation operations process the data records/documents and return computed
results.
• It collects values from various documents and groups them together and then
performs different types of operations on that grouped data like sum, average,
minimum, maximum, etc to return a computed result.
• the aggregation pipeline consists of stages and each stage transforms the
document.
16.
17. Aggregation Syntax
Basic syntax of aggregate() method is as follows −
db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
Example:
db.collection.aggregate([ {stage1}, {stage2}, {stage3}...])
• There is a set of possible stages and each of those is taken as a set of documents
as an input and produces a resulting set of documents (or the final resulting JSON
document at the end of the pipeline)
18. Stages
Stages: Each stage starts from stage operators which are:
•$match: It is used for filtering the documents can reduce the amount of documents
that are given as input to the next stage.
•$project: It is used to select some specific fields from a collection.
•$sort: It is used to sort the document that is rearranging them
•$limit: It is used to pass first n number of documents thus limiting them