0% found this document useful (0 votes)
4 views

MongoDB features

The document provides a comprehensive overview of MongoDB, detailing its basic, medium, advanced, and cutting-edge features, including document-oriented storage, collections, queries, indexes, aggregation framework, transactions, data modeling, change streams, GridFS, full-text search, sharding, and backup/restore functionalities. Each feature includes its version, explanation, use case, sub-concepts, and relevant code snippets. Additionally, it highlights the cloud-based MongoDB Atlas service for simplified database management.

Uploaded by

camilo.ms
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

MongoDB features

The document provides a comprehensive overview of MongoDB, detailing its basic, medium, advanced, and cutting-edge features, including document-oriented storage, collections, queries, indexes, aggregation framework, transactions, data modeling, change streams, GridFS, full-text search, sharding, and backup/restore functionalities. Each feature includes its version, explanation, use case, sub-concepts, and relevant code snippets. Additionally, it highlights the cloud-based MongoDB Atlas service for simplified database management.

Uploaded by

camilo.ms
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

MONGO DB

1. Basic Features

a. Document-Oriented Storage

 Version: 1.0 (2009)


 Part of: MongoDB
 Explanation: MongoDB is a NoSQL database that stores data in
flexible, JSON-like documents, allowing for more complex data
structures.
 Use Case: Storing hierarchical data without needing to define a fixed
schema.
 Sub-Concepts:
o BSON: Binary representation of JSON-like documents.
 Code Snippet:
javascript
Copy code
const document = {
name: "Alice",
age: 30,
hobbies: ["reading", "traveling"]
};
db.users.insertOne(document);

b. Collections

 Version: 1.0 (2009)


 Part of: MongoDB
 Explanation: Collections are groups of MongoDB documents, similar
to tables in relational databases.
 Use Case: Organizing documents into logical groups.
 Sub-Concepts:
o Default Collections: Each collection automatically has an _id
field as the primary key.
 Code Snippet:
javascript
Copy code
db.createCollection("users");

c. Queries

 Version: 1.0 (2009)


 Part of: MongoDB
 Explanation: MongoDB provides a rich query language to retrieve and
manipulate data.
 Use Case: Finding documents based on various criteria.
 Sub-Concepts:
o Find Queries: Using find() and findOne().
o Projection: Selecting specific fields to return.
 Code Snippet:
javascript
Copy code
db.users.find({ age: { $gt: 25 } }, { name: 1, age: 1 });

2. Medium Features

a. Indexes

 Version: 1.0 (2009)


 Part of: MongoDB
 Explanation: Indexes improve the speed of data retrieval operations
on a database collection.
 Use Case: Enhancing query performance.
 Sub-Concepts:
o Single Field Index: Index on a single field.
o Compound Index: Index on multiple fields.
o Text Index: Full-text search capabilities.
 Code Snippet:
javascript
Copy code
db.users.createIndex({ name: 1 }); // Creates an index on the 'name' field
b. Aggregation Framework

 Version: 2.2 (2013)


 Part of: MongoDB
 Explanation: The aggregation framework allows you to process data
and return computed results.
 Use Case: Performing complex data analysis and transformations.
 Sub-Concepts:
o Pipeline: Sequence of stages to process data.
o Operators: $match, $group, $sort, $project.
 Code Snippet:
javascript
Copy code
db.orders.aggregate([
{ $match: { status: "shipped" } },
{ $group: { _id: "$customerId", total: { $sum: "$amount" } } }
]);

c. Transactions

 Version: 4.0 (2018)


 Part of: MongoDB
 Explanation: Multi-document transactions allow for executing multiple
operations across multiple documents with ACID properties.
 Use Case: Ensuring consistency when performing multiple operations.
 Sub-Concepts:
o Start Transaction: session.startTransaction().
o Commit Transaction: session.commitTransaction().
o Abort Transaction: session.abortTransaction().
 Code Snippet:
javascript
Copy code
const session = client.startSession();
session.startTransaction();
try {
await db.collection('users').insertOne({ name: 'Alice' }, { session });
await db.collection('orders').insertOne({ userId: '123', amount: 100 },
{ session });
await session.commitTransaction();
} catch (error) {
await session.abortTransaction();
} finally {
session.endSession();
}

3. Advanced Features

a. Data Modeling

 Version: 1.0 (2009)


 Part of: MongoDB
 Explanation: Designing how data is stored and related in MongoDB.
 Use Case: Structuring data according to application needs.
 Sub-Concepts:
o Embedded Documents: Storing related data within a single
document.
o References: Linking documents using ObjectIDs.
 Code Snippet:
javascript
Copy code
const userSchema = new mongoose.Schema({
name: String,
age: Number,
addresses: [{ street: String, city: String }]
});

b. Change Streams

 Version: 3.6 (2017)


 Part of: MongoDB
 Explanation: Change streams allow applications to subscribe to real-
time changes in the database.
 Use Case: Building reactive applications that respond to database
changes.
 Common Libraries:
o MongoDB Driver: Native support for change streams.
 Code Snippet:
javascript
Copy code
const changeStream = db.collection('users').watch();

changeStream.on('change', (change) => {


console.log(change);
});

c. GridFS

 Version: 1.0 (2009)


 Part of: MongoDB
 Explanation: GridFS is a specification for storing and retrieving large
files, such as images and videos, in MongoDB.
 Use Case: Handling files larger than the BSON document size limit (16
MB).
 Sub-Concepts:
o GridFS Buckets: Mechanism to store files.
 Code Snippet:
javascript
Copy code
const bucket = new mongoose.mongo.GridFSBucket(db);

const uploadStream = bucket.openUploadStream('myFile.txt');


uploadStream.end('Hello, world!');

d. Full-Text Search

 Version: 2.4 (2013)


 Part of: MongoDB
 Explanation: Full-text search capabilities allow for searching text
fields within documents.
 Use Case: Implementing search functionality in applications.
 Common Libraries:
o MongoDB Atlas Search: Provides enhanced text search
capabilities.
 Code Snippet:
javascript
Copy code
db.articles.createIndex({ title: 'text', content: 'text' });
db.articles.find({ $text: { $search: "MongoDB" } });

e. Sharding

 Version: 1.6 (2009)


 Part of: MongoDB
 Explanation: Sharding is a method for distributing data across
multiple servers to ensure scalability and performance.
 Use Case: Handling large datasets by distributing them across
multiple nodes.
 Sub-Concepts:
o Shard Key: The field used to distribute documents across
shards.
 Code Snippet:
javascript
Copy code
sh.shardCollection("mydb.users", { userId: 1 });

f. Backup and Restore

 Version: 3.0 (2015)


 Part of: MongoDB
 Explanation: MongoDB provides various tools for backing up and
restoring data.
 Use Case: Ensuring data safety and recovery.
 Common Libraries:
o mongodump and mongorestore: Tools for backing up and
restoring.
 Code Snippet:
bash
Copy code
mongodump --db mydb --out /path/to/backup
mongorestore --db mydb /path/to/backup/mydb
4. Extra (Cutting Edge) Features

a. Transactions

 Version: 4.0 (2018)


 Part of: MongoDB
 Explanation: Multi-document transactions allow for executing multiple
operations across multiple documents with ACID properties.
 Use Case: Ensuring consistency when performing multiple operations.
 Sub-Concepts:
o Start Transaction: session.startTransaction().
o Commit Transaction: session.commitTransaction().
o Abort Transaction: session.abortTransaction().
 Common Libraries:
o Mongoose: Supports transactions in its APIs.
 Code Snippet:
javascript
Copy code
const session = client.startSession();
session.startTransaction();
try {
await db.collection('users').insertOne({ name: 'Alice' }, { session });
await db.collection('orders').insertOne({ userId: '123', amount: 100 },
{ session });
await session.commitTransaction();
} catch (error) {
await session.abortTransaction();
} finally {
session.endSession();
}

b. Aggregation Framework

 Version: 2.2 (2013)


 Part of: MongoDB
 Explanation: The aggregation framework allows you to process data
and return computed results.
 Use Case: Performing complex data analysis and transformations.
 Sub-Concepts:
o Pipeline: Sequence of stages to process data.
o Operators: $match, $group, $sort, $project.
 Common Libraries:
o Mongoose: Provides an aggregation method for models.
 Code Snippet:
javascript
Copy code
db.orders.aggregate([
{ $match: { status: "shipped" } },
{ $group: { _id: "$customerId", total: { $sum: "$amount" } } }
]);

c. Change Streams

 Version: 3.6 (2017)


 Part of: MongoDB
 Explanation: Change streams allow applications to subscribe to real-
time changes in the database.
 Use Case: Building reactive applications that respond to database
changes.
 Common Libraries:
o MongoDB Driver: Native support for change streams.
 Code Snippet:
javascript
Copy code
const changeStream = db.collection('users').watch();

changeStream.on('change', (change) => {


console.log(change);
});

d. MongoDB Atlas

 Version: Cloud-based (2016)


 Part of: MongoDB
 Explanation: MongoDB Atlas is a fully managed cloud database
service that hosts MongoDB databases.
 Use Case: Simplifying database management, scaling, and
monitoring.
 Sub-Concepts:
o Serverless Instances: On-demand scaling.
o Global Clusters: Multi-region deployments.
 Code Snippet:
javascript
Copy code
const MongoClient = require('mongodb').MongoClient;

const uri = "your-atlas-connection-string";


const client = new MongoClient(uri, { useNewUrlParser: true,
useUnifiedTopology: true });

client.connect(err => {
const collection = client.db("test").collection("devices");
client.close();
});

e. Full-Text Search

 Version: 2.4 (2013)


 Part of: MongoDB
 Explanation: Full-text search capabilities allow for searching text
fields within documents.
 Use Case: Implementing search functionality in applications.
 Common Libraries:
o MongoDB Atlas Search: Provides enhanced text search
capabilities.
 Code Snippet:
javascript
Copy code
db.articles.createIndex({ title: 'text', content: 'text' });
db.articles.find({ $text: { $search: "MongoDB" } });

You might also like