•See Section 20.3.2, “Download and Import world_x Database”  for instructions to recreate the world_x schema. 

## 20.3.3.6 Create and Drop Indexes 

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-js> db.countryinfo.find("demographics.Population < 100") ...[ output removed ] 8 documents in set (0.00 sec) 

The createIndex()  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. 

## Add a Nonunique Index 

To create a nonunique index, pass an index name and the index information to the createIndex() 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-js> 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. 

## Add a Unique Index 

To create a unique index, pass an index name, the index definition, and the index type “unique”  to the createIndex()  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-js> db.countryinfo.createIndex("name", {"fields": [{"field": "$.Name", "type": "TEXT(40)", "required": true}], "unique": true}) 

## Drop an Index 

To drop an index, pass the name of the index to drop to the dropIndex()  method. For example, you can drop the “popul”  index as follows: 

## Related Information 

mysql-js> db.countryinfo.dropIndex("popul") 

•See Indexing Collections for more information. 

