Learning MongoDB

0% completed

Previous
Next
Get an Index

Indexes in MongoDB are crucial for optimizing query performance. Once indexes are created, it's important to be able to retrieve and review them to ensure they are being used effectively and to manage them properly. MongoDB provides commands to list and get detailed information about the indexes on a collection.

Syntax for Retrieving Indexes

To retrieve indexes in MongoDB, you use the getIndexes() method. The basic syntax for retrieving indexes is:

db.collection.getIndexes()

Parameters and Return Value

The getIndexes() method does not take any parameters and returns an array of documents, each representing an index on the collection. Each document contains details about the index, such as the fields indexed, the index name, and any options specified when the index was created.

Example Setup

First, let's insert some documents into the products collection and create a couple of indexes:

db.products.insertMany([ { name: "Apple", category: "Fruit", price: 1.2 }, { name: "Carrot", category: "Vegetable", price: 0.8 }, { name: "Banana", category: "Fruit", price: 1.1 }, { name: "Broccoli", category: "Vegetable", price: 1.5 }, { name: "Grapes", category: "Fruit", price: 2.0 } ]) db.products.createIndex({ name: 1 }, { unique: true, name: "uniqueNameIndex" }) db.products.createIndex({ category: 1, price: -1 })

Example 1: Retrieving All Indexes

Retrieve all indexes on the products collection.

db.products.getIndexes()

Explanation:

  • db.products.getIndexes(): This command retrieves an array of documents, each representing an index on the products collection. The output includes information about the index fields, index names, and any options specified during index creation.

Example Output:

[ { "v": 2, "key": { "_id": 1 }, "name": "_id_", "ns": "test.products" }, { "v": 2, "unique": true, "key": { "name": 1 }, "name": "uniqueNameIndex", "ns": "test.products" }, { "v": 2, "key": { "category": 1, "price": -1 }, "name": "category_1_price_-1", "ns": "test.products" } ]

Explanation:

  • Each document in the output represents an index.
  • The key field indicates the fields indexed and their order (1 for ascending, -1 for descending).
  • The name field shows the name of the index.
  • The unique field, if present, indicates that the index enforces uniqueness.
  • The ns field shows the namespace (database and collection) to which the index belongs.

Example 2: Retrieving Indexes with Additional Options

Create a partial index and retrieve all indexes, including this one.

First, create a partial index:

db.products.createIndex( { category: 1, price: -1 }, { partialFilterExpression: { price: { $gt: 1.0 } }, name: "partialIndex" } )

Then, retrieve all indexes:

db.products.getIndexes()

Explanation:

  • It creates a partial index on the category and price fields, only indexing documents where price is greater than 1.0. The index is named "partialIndex".
  • db.products.getIndexes(): Retrieves all indexes on the products collection, including the newly created partial index.

Example Output:

[ { "v": 2, "key": { "_id": 1 }, "name": "_id_", "ns": "test.products" }, { "v": 2, "unique": true, "key": { "name": 1 }, "name": "uniqueNameIndex", "ns": "test.products" }, { "v": 2, "key": { "category": 1, "price": -1 }, "name": "category_1_price_-1", "ns": "test.products" }, { "v": 2, "key": { "category": 1, "price": -1 }, "name": "partialIndex", "ns": "test.products", "partialFilterExpression": { "price": { $gt: 1.0 } } } ]

Explanation:

  • The partialFilterExpression field in the output for the "partialIndex" shows the filter criteria used to create the partial index.

Retrieving indexes in MongoDB is essential for managing and optimizing database performance. By using the getIndexes() method, you can list all indexes on a collection and review their details. This lesson covered the syntax for retrieving indexes, detailed explanations of the output, and practical examples, providing a comprehensive understanding of how to manage and utilize indexes effectively in MongoDB.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next