Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

MongoDB Indexes

MongoDB Indexes

Indexes can usually greatly improve query efficiency. Without indexes, MongoDB must scan every document in a collection when reading data and select those records that match the query conditions.

This kind of query that scans the whole collection is very inefficient, especially when dealing with large amounts of data, where a query may take tens of seconds or even minutes, which is very fatal to website performance.

An index is a special data structure. An index is stored in a data collection that is easy to traverse and read. An index is a structure that sorts the values of one or more columns in a database table.

In MongoDB, common index types include:


Creating indexes

MongoDB uses the createIndex() method to create indexes.

Note that before version 3.0.0, the index creation method was db.collection.ensureIndex(), and later versions use the db.collection.createIndex() method; ensureIndex() can still be used, but is only an alias for createIndex().

Syntax

The basic syntax of the createIndex() method is as follows:



db.collection.createIndex( keys, options )

The options parameter is an object that can contain various configuration options. The following are some commonly used options:

Example

// Create an ascending index on the age field

db.

myCollection

.

createIndex

(

{

age

:

1

}

)

;

// Create a text index on the name field

db.

myCollection

.

createIndex

(

{

name

:

"text"

}

)

;

Example

Index creation:

Example

// Create a unique index

db.

collection

.

createIndex

(

{

field

:

1

}

,

{

unique

:

true

}

)

// Create an index that runs in the background

db.

collection

.

createIndex

(

{

field

:

1

}

,

{

background

:

true

}

)

// Create a sparse index

db.

collection

.

createIndex

(

{

field

:

1

}

,

{

sparse

:

true

}

)

// Create a text index and specify weights

db.

collection

.

createIndex

(

{

field

:

"text"

}

,

{

weights

:

{

field

:

10

}

}

)

Create a geospatial index

For fields that store geographic location data, you can use the 2dsphere or 2d index type to create a geospatial index.

// 2dsphere index, suitable for spherical geospatial data

db.

collection

.

createIndex

(

{

location

:

"2dsphere"

}

)

// 2d index, suitable for planar geospatial data

db.

collection

.

createIndex

(

{

location

:

"2d"

}

)

Create a hashed index

Starting from MongoDB 3.2, you can use hashed indexes to hash a field in order to support lookups over a wide range of values.



db.collection.createIndex( { field: "hashed" } )

View indexes

Use the getIndexes() method to view all indexes in a collection:



db.collection.getIndexes()

Delete indexes

Use the dropIndex() or dropIndexes() method to delete indexes:

Example

// Delete a specified index

db.

collection

.

dropIndex

(

"indexName"

)

// Delete all indexes

db.

collection

.

dropIndexes

(

)

Indexing strategies

When creating indexes, you need to consider the following factors:

Index optimization

When optimizing indexes, you can consider the following approaches:

Notes

By designing and using indexes properly, you can greatly improve the query performance and response speed of a MongoDB database, thereby better supporting the needs of your application.