Skip to content

This repository is about what are indexes and Transactions in MongoDb. How can we get benefit from creating indexes and so much. Happy learning and coding!

Notifications You must be signed in to change notification settings

Farooq85-dev/Indexes-Transactions-In-MongoDb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

Indexes-&-Transactions-In-MongoDb

What are indexes in MongoDb?

Indexes in MongoDB improve the performance of search operations by providing efficient ways to find data. Without indexes, MongoDB would need to scan every document in a collection to fulfill a query, which can be slow, especially with large datasets.

Why Use Indexes in MongoDB?

  1. Speed: They speed up query performance by reducing the amount of data MongoDB needs to scan.
  2. Efficiency: They help optimize query operations such as find(), update(), and delete().
  3. Sorting: They enable efficient sorting of documents when used with the $sort stage in aggregation.

Types of Indexes in MongoDB:

  1. Single Field Index: Indexes a single field of a document.
  2. Compound Index: Indexes multiple fields of a document, useful when queries need to filter or sort based on multiple fields.
  3. Multikey Index: Used when the indexed field is an array.
  4. Text Index: Enables full-text search on string fields.

How to create an Single Field Index (SFI)?

db.collection.createIndex({ "email": 1 })  // 1 for ascending order

How to create Compound Index (CI)?

db.collection.createIndex({ "name": 1, "age": -1 })  // 1 for ascending, -1 for descending

How to create a Text Index (TI)?

db.products.createIndex({ "description": "text" })

What is Transactions in MongoDb?

A transaction in MongoDB allows you to execute multiple operations across multiple documents and collections in a way that ensures atomicity — either all operations in the transaction succeed, or none of them do. This is crucial for maintaining data consistency in applications where multiple operations need to be performed together.

When to Use Transactions:

  1. Multiple Operations: When multiple operations need to be treated as a single unit of work.
  2. Cross-Collection Operations: When working with multiple collections within a single operation.
  3. Atomicity: When you need to ensure that changes are rolled back if something fails.

ACID Properties in Transactions:

  1. Atomicity: All operations within a transaction are treated as a single unit. If one operation fails, the transaction is rolled back.
  1. Consistency: Ensures that the database state is consistent before and after the transaction.

  2. Isolation: Ensures that operations in a transaction are isolated from other transactions.

  3. Durability: Ensures that once a transaction is committed, it will persist, even in the case of system failures.

About

This repository is about what are indexes and Transactions in MongoDb. How can we get benefit from creating indexes and so much. Happy learning and coding!

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published