0% completed
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.
To retrieve indexes in MongoDB, you use the getIndexes()
method. The basic syntax for retrieving indexes is:
db.collection.getIndexes()
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.
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 })
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:
key
field indicates the fields indexed and their order (1 for ascending, -1 for descending).name
field shows the name of the index.unique
field, if present, indicates that the index enforces uniqueness.ns
field shows the namespace (database and collection) to which the index belongs.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:
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:
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.
.....
.....
.....