How To Create an Index for a MongoDB Collection in Python

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

If you want to improve your query performance, it can be helpful to create indexes for your MongoDB collections. Indexes help make query requests more efficient because they store data in a way that makes it quick and easy to traverse. With the help of an index, MongoDB can locate documents that match your query criteria without performing a full collection scan. Instead, MongoDB uses the index, which allows it to limit the number of documents it examines. In this article, we’ll provide instructions for creating indexes for a MongoDB collection using Python.

>NOTE: We’ll be using the create_index() method for indexes in this tutorial, as the ensureIndex() API (or ensure_index() in PyMongo) has been deprecated since MongoDB 3.0.

Prerequisites

Before we turn our attention to the Python code, let’s review the prerequisites that will be needed for this task:

  • You’ll need to make sure the Python interpreter has been properly installed. Keep in mind that all the examples in this article use Python 3.

  • You’ll need to install PyMongo– the low-level Python driver for MongoDB– using the PIP package manager:

1
pip3 install pymongo
  • You’ll also need to install the MongoDB server application. You can use the mongo --version command in a terminal, or you can simply enter the Mongo shell in a terminal by typing mongo and pressing Return.

  • Before you begin, be sure to have a database and a collection available. The collection should contain documents whose fields you can use to create indexes.

  • It’s helpful to have a working knowledge of Python and its syntax to follow along with the examples in this article.

Instantiate a MongoDB collection object in Python that will be used to created indexes

Now that we’ve gone over the key system requirements for this task, we can dive into the code. Let’s start by importing the MongoClient class from the PyMongo library in a Python script. After that, we’ll create a new instance of the client driver:

1
2
3
4
5
# import the PyMongo MongoClient class
from pymongo import MongoClient

# create a client instance of MongoClient
mongo_client = MongoClient('mongodb://localhost:27017')

Next, let’s use the client instance to create a database and collection instance that we’ll be using to make API calls:

1
2
3
4
5
# create a new database instance
db = mongo_client.some_database

# create a new collection instance from db
col = db.some_collection

It’s not a problem if the database or collection you specify doesn’t yet exist on the MongoDB server– once you make API calls to create indexes on them, MongoDB will create the database or collection on the MongoDB server.

Different types of indexes for a MongoDB collection

There are many different types of indexes in MongoDB that can be used for indexing a collection. Let’s take a brief look at each one:

Single-Field Index: As the name implies, this is an index on a single field of a document. For this type of index, the sort order doesn’t affect how efficiently documents are queried. It can be traversed in either descending or ascending order.

Compound Index: This type of index can be created on multiple fields. For a compound index, the order that the fields are listed impacts sorting.

Multikey Index: This type of index is used to index data that has been stored in arrays.

Geospatial Index: This is an index that’s used for indexing geospatial coordinate data, which can take the form of coordinate pairs or GeoJSON objects.

Hashed Indexes: This kind of index is used for large collections that are sharded and spread out over several machines. The hash of the value of a field is indexed instead of the value itself.

Create a simple, single-field index in PyMongo

In the following example, we’ll show a basic API call to the create_index() method. All you need to pass is a string that represents the index name as the parameter:

1
col.create_index("an_index")

If you were to print out the results of this API call, it would return a string containing the index name followed by an underscore, and the integer “1” (_1). This indicates that the index will be sorted in the default ascending order.

Create an index for a MongoDB collection, and specify the order

When you use PyMongo to create an index, you can pair the index name with either a 1 or a -1 integer value to explicitly set the sort order to ascending or descending, respectively.

The syntax for this API call is a bit different from the Mongo Shell. The PyMongo API call to create_index() requires that you pass the parameters as a tuple object nested inside of a list. An example of this syntax is shown below:

1
2
3
# create an index in descending order, and return a result
resp = col.create_index([ ("field_to_index", -1) ])
print ("index response:", resp)

Like the last example, the response returned by the method will be a string containing the field name that was indexed, followed by an underscore (_) and a -1 because we specified that the collection would be indexed in a descending order.

Use DESCENDING or ASCENDING when indexing MongoDB collections instead of -1 and 1 respectively

In our next example, we’ll make the create_index() call more readable by passing the pymongo.DESCENDING or pymongo.ASCENDING attributes instead of a -1 or 1. To do this, we need to make sure to either import all of the classes and attributes of the pymongo library:

1
from pymongo import *

Another option is to explicitly import them by name when we import the MongoClient class:

1
from pymongo import MongoClient, ASCENDING, DESCENDING

Create a compound index in Python using more than one field

You can index multiple fields in a collection by passing a Python list like we did in an earlier example; however, this time we’ll add more tuple pairs to the list, resulting in a compound index:

1
2
3
4
5
6
7
8
resp = col.create_index(
[
("field_to_index", 1),
("second_field_indexed", -1)
]
)

print ("index response:", resp)

Conclusion

Creating indexes for MongoDB collections is a simple way to make the process of querying and sorting documents more efficient. As you could see in the examples we discussed, the PyMongo version of the create_index() method is quite similar to its Mongo Shell command equivalent, except that the index-order tuple pair is passed inside of a Python list [] instead of brackets {}. Using the instructions provided in this article, you’ll be well-prepared to create an index for MongoDB using Python.

Just the Code

In this article, we looked at the example code one segment at a time. Shown below is the Python script in its entirety:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env python3
#-*- coding: utf-8 -*-

# import PyMongo and a few of its classes
from pymongo import MongoClient, ASCENDING, DESCENDING

# create a client instance of MongoClient
mongo_client = MongoClient('mongodb://localhost:27017')

# very simple create_index()
col.create_index("an_index")

# create an index in descending order
resp = col.create_index([ ("field_to_index", -1) ])
print ("index response:", resp)

# create an index in ASCENDING order
resp = col.create_index([ ("field_to_index", ASCENDING) ])
print ("index response:", resp)

# create a compund index
resp = col.create_index(
[
("field_to_index", 1),
("second_field_indexed", DESCENDING)
]
)

print ("index response:", resp)

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.