---
title: "What is the purpose of the MongoDB index and how do you create one?"  
description: "What is the purpose of the MongoDB index and how do you create one?"  
author: "Revati S Misra"  
published: 2023-05-16  
updated: 2023-05-18  
canonical: https://www.mindstick.com/forum/158371/what-is-the-purpose-of-the-mongodb-index-and-how-do-you-create-one  
category: "mongodb"  
tags: ["database", "mongodb", "mongodb query"]  
reading_time: 2 minutes  

---

# What is the purpose of the MongoDB index and how do you create one?

What are some [common](https://www.mindstick.com/articles/23170/10-most-common-accounting-mistakes-of-small-business) use cases for [MongoDB](https://www.mindstick.com/articles/337565/understanding-document-stores-a-look-at-mongodb-vs-couchdb) and how can it be used in a real-[world](https://yourviews.mindstick.com/view/87468/defining-humanity-as-given-in-sanatan-dharma-best-in-world) [application](https://www.mindstick.com/articles/12824/calculator-application-in-android)?

## Replies

### Reply by Aryan Kumar

\
A MongoDB index is a data structure that helps MongoDB find documents quickly. Indexes are created on one or more fields in a collection. When you create an index, MongoDB stores the values of the indexed fields in a separate data structure. This data structure is called the B-tree.

When you run a query on a collection with an index, MongoDB can use the index to find the documents that match the query without having to scan the entire collection. This can make queries much faster.

There are two types of indexes in MongoDB:

- **Single-field indexes:** Single-field indexes are created on a single field in a collection.
- **Multi-field indexes:** Multi-field indexes are created on multiple fields in a collection.

To create an index in MongoDB, you use the following command:

Code snippet

```plaintext
db.collection.createIndex( { <field>: <direction> } )
```

where:

- <field> is the name of the field to index.
- <direction> is the direction of the index. The possible values are 1 for ascending and -1 for descending.

For example, the following command creates a single-field index on the name field in the users collection:

Code snippet

```plaintext
db.users.createIndex( { name: 1 } )
```

This index will be used to find documents in the users collection where the name field is in ascending order.

You can also create multi-field indexes. For example, the following command creates a multi-field index on the name and age fields in the users collection:

Code snippet

```plaintext
db.users.createIndex( { name: 1, age: 1 } )
```

This index will be used to find documents in the users collection where the name field is in ascending order and the age field is in ascending order.

Indexes can improve the performance of queries, but they also take up space. It is important to create indexes only on fields that are frequently used in queries


---

Original Source: https://www.mindstick.com/forum/158371/what-is-the-purpose-of-the-mongodb-index-and-how-do-you-create-one

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
