Indexes are used to find documents with specific field values quickly. Without an index, MySQL must begin with the first document and then read through the entire collection to find the relevant fields. The larger the collection, the more this costs. If a collection is large and queries on a specific field are common, then consider creating an index on a specific field inside a document.
For example, the following query performs better with an index on the Population field:
mysql-py> db.countryinfo.find("demographics.Population < 100")
...[output removed]
8 documents in set (0.00 sec)
The create_index()
method creates an index that you can define with a JSON document that specifies which fields to use. This section is a high level overview of indexing. For more information see Indexing Collections.
To create a nonunique index, pass an index name and the index information to the create_index()
method. Duplicate index names are prohibited.
The following example specifies an index named popul
, defined against the Population
field from the demographics
object, indexed as an Integer
numeric value. The final parameter indicates whether the field should require the NOT NULL
constraint. If the value is false
, the field can contain NULL
values. The index information is a JSON document with details of one or more fields to include in the index. Each field definition must include the full document path to the field, and specify the type of the field.
mysql-py> db.countryinfo.createIndex("popul", {fields:
[{field: '$.demographics.Population', type: 'INTEGER'}]})
Here, the index is created using an integer numeric value. Further options are available, including options for use with GeoJSON data. You can also specify the type of index, which has been omitted here because the default type “index” is appropriate.
To create a unique index, pass an index name, the index definition, and the index type “unique” to the create_index()
method. This example shows a unique index created on the country name ("Name"
), which is another common field in the countryinfo
collection to index. In the index field description, "TEXT(40)"
represents the number of characters to index, and "required": True
specifies that the field is required to exist in the document.
mysql-py> db.countryinfo.create_index("name",
{"fields": [{"field": "$.Name", "type": "TEXT(40)", "required": True}], "unique": True})
To drop an index, pass the name of the index to drop to the drop_index()
method. For example, you can drop the “popul” index as follows:
mysql-py> db.countryinfo.drop_index("popul")
See Indexing Collections for more information.
See Defining an Index for more information on the JSON document that defines an index.
See Collection Index Management Functions for the full syntax definition.