MongoDB – db.collection.createIndexes() Method
The createIndexes() method creates one or more indexes on the specified collection. It is used to create one or more indexes based on the field of the document. Using this method we can create different types of indexes like text index, 2dsphere index, 2d index, etc. It takes three parameters first one is a document that contains the field and value pairs where the field is the index key, and the value describes the type of index for that field, and others are optional.
- If the index is already created or exists then this method does not recreate the existing index.
- You can hide and unhide an index using hideIndex() and unhideIndex() method.
Syntax:
db.Collection_name.createIndexes(
keysPatterns : [{Field_name:1/-1},…….],
options : <document>,
commitQuorum : <string or integer>
)
Parameters:
- The first parameter is an array of one or more documents that contain the field and value pairs where the field is the index key, and the value describes the type of index for that field. Or in other words, this parameter is an array that contains the index specification documents and each document contains the field and value pairs where the field is the index key, and the value describes the type of index for that field. For an ascending index on a field, specify the value 1, and for descending index, specify the value -1.
- Others are optional.
Optional Parameters:
- Options: It is a set of options that controls the creation of the index. The type of this parameter is a document.
- commitQuorum: It is the minimum number of data-bearing voting replica set members.
Options:
There is a set of options in the options document that controls the creation of indexes. Different index types can have additional options specific for that type. The following options are available:
- background: The type of this option is boolean. This option takes the value true or false if it is true, then it directs MongoDB to build the index in the background. The default value of this option is false. Background builds do not block operations on the collection.
- unique: The type of this option is boolean. It specifies that a unique index is used for each index listed in the keyPatterns array. Insertion or modification of documents where the index key value refers to an existing value in the index would not be accepted by specific indexes. To create a unique index set value to true. The default value is false.
- name: The type of this option is a string. It takes a string that is the name of the index. If it is not specified, then MongoDB creates a name by concatenating the names of the indexed fields and the sort order.
- partialFilterExpression: The type of this option is document. If it is specified, the indexes only reference documents that match the query expression.
- sparse: The type of this option is boolean. The default value of this option is false. If we set its value to true then the indexes only reference documents with the specified fields.
- expireAfterSeconds: The type of this option is an integer. It specifies a value to monitor how long MongoDB holds documents in this collection, in seconds.
- hidden: The type of this option is boolean. The default value of this option is false. A flag that specifies if the index in the query planner is hidden. As a part of the query plan selection, a hidden index is not evaluated.
- storageEngine: The type of this option is document. It allows users to configure the storage engine for the created indexes.
Some indexes may have additional options that are specified for that type only like:
For text indexes:
All these parameters are optional:
- weights: It is of document type and contains the field and weight pairs for text index. The default value of this parameter is 1.
- default_language: It is of string type and specifies the language that determines the list of stop words and the rules for stemmer and tokenizer.
- language_override: It is of string type and specifies the name of the fields in the document that contains the override language for the document.
- textIndexVersion: It is of integer type and specifies the text index version number.
For 2dsphere Indexes:
2dsphereIndexVersion: it is of integer type and specifies the 2dsphere index version number. It is an optional parameter.
For 2d Indexes:
All these parameters are optional:
- bits: It is of integer type and specifies the number of precision of the stored geohash value of the location data. The default value of this parameter is 26.
- min: It is of number type and specifies the lower inclusive boundary for the longitude and latitude values. The default value of this parameter is -180.0.
- max: It is of number type and specifies the higher inclusive boundary for the longitude and latitude values. The default value of this parameter is -180.0.
For geoHaystack Indexes:
bucketSize: It is of number type and specifies the number of units within which to group the local values. The value of this parameter must be greater than 0.
For wildcard indexes:
wildcardProjection: It is of document type and allows users to include or exclude specific field paths from the wildcard index. It is only valid if you are creating a wildcard index. It is an optional parameter.
Examples:
In the following examples, we are working with:
Database: gfg
Collections: employee
Document: Three documents contains the details of the employees
- Create an ascending index on the single field name:
db.employee.createIndexes([{name:1}])
Here, we create an index on the name field in ascending order.
- Create a descending index on the single field name:
db.employee.createIndexes([{name:-1}])
Here, we create an index on the name field in descending order.
- Create an index on the multiple fields: Ascending index on the name, and Descending index on language:
db.employee.createIndexes([{name:1,department:-1}]
Here, we create the indexes on multiple fields, i.e. ascending index on the name field, and descending index on department field.
- Creates the ascending unique indexes on the joinYear field:
db.employee.createIndexes([{joinYear:1}],{unique:true})
Here, we create the ascending unique indexes on the joinYear field by setting the value of the unique parameter to true.
- Creates the descending sparse indexes on the joinYear field:
db.employee.createIndexes([{joinYear:-1}],{sparse:true})
Here, we create the descending sparse indexes on the joinYear field by setting the value of the sparse parameter to true.
- Creates a wildcard index on the name field:
db.employee.createIndexes([{"name.$**":1}])
Contact Us