Create index with name in mongodb
Docs Home → MongoDB Manual Show db.collection.createIndex(keys, options, commitQuorum) Importantmongosh MethodThis page documents a
For MongoDB API drivers, refer to the language-specific MongoDB driver documentation. For the legacy
Creates indexes on collections. To minimize the impact of building an index on replica sets and sharded clusters, use a rolling index build procedure as described on Rolling Index Builds on Replica Sets.
The Multiple index options can be specified in the same document. However, if you specify multiple option documents the
Consider the following
If the options specification had been split into multiple documents
like this: The following options are available for all index types unless otherwise specified:
document Optional. If specified, the index only references documents that match the filter expression. See Partial Indexes for more information. A filter expression can include:
You can specify a
boolean Optional. If The following index types are sparse by default and ignore this option:
For a compound index that includes Changed in version 3.2: Starting in MongoDB 3.2, MongoDB provides the option to create partial indexes. Partial indexes offer a superset of the functionality of sparse indexes. If you are using MongoDB 3.2 or later, partial indexes should be preferred over sparse indexes.
integer Optional. Specifies a value, in seconds, as a TTL to control how long MongoDB retains documents in this collection. See Expire Data from Collections by Setting TTL for more information on this functionality. This applies only to TTL indexes. hidden boolean Optional. A flag that determines whether the index is hidden from the query planner. A hidden index is not evaluated as part of the query plan selection. Default is To use the New in version 4.4.
document Optional. Allows users to configure the storage engine on a per-index basis when creating an index. The
Storage engine configuration options specified when creating indexes are validated and logged to the oplog during replication to support replica sets with members that use different storage engines. WarningCollation is not supported in MongoDB 3.2 and earlier versions. In MongoDB 3.2 and earlier versions, do not create indexes with the unsupported collation option as this will block the upgrade to 3.4, which enforces a stricter validation of index options.
The following indexes only support simple binary comparison and do not support collation:
TipTo create a If you have specified a collation at the collection level, then:
TipBy specifying a collation You can create multiple indexes on the same key(s) with different collations. To create indexes with the same key pattern but different collations, you must supply unique index names. To use an index for string comparisons, an operation must also specify the same collation. That is, an index with a collation cannot support an operation that performs string comparisons on the indexed fields if the operation specifies a different collation. For example, the collection
The following query operation, which specifies the same collation as the index, can use the index:
However, the following query operation, which by default uses the "simple" binary collator, cannot use the index:
For a compound index where the index prefix keys are not strings, arrays, and embedded documents, an operation that specifies a different collation can still use the index to support comparisons on the index prefix keys. For example, the collection
The following operations, which use
The following operation, which uses
The following options are available for text indexes only:
The following option is available for 2dsphere indexes only:
The following options are available for 2d indexes only:
The following option is available for geoHaystack indexes only:
NoteRemoved in MongoDB 5.0The following option is available for wildcard indexes only:
Changed in version 4.2. For featureCompatibilityVersion For featureCompatibilityVersion For more information on the locking behavior of If you call
With the exception of the collation option, if you create an index with one set of index options and then try to recreate the same index but with different index options, MongoDB will not change the options nor recreate the index. The hidden option can be changed without dropping and recreating the index. See Hidden Option. To change the other index options, drop the existing index with You can create multiple indexes on the same key(s) with different collations. To create indexes with the same key pattern but different collations, you must supply unique index names. New in version 4.4. NoteTo hide or unhide existing indexes, you can use the following
For example,
TipSee also:For MongoDB 2.6 through MongoDB versions with
featureCompatibilityVersion (fCV) set to Changed in version 4.4. Starting in MongoDB 4.4, you can create collections and indexes inside a multi-document transaction if the transaction is not a cross-shard write transaction. To use
TipSee also:The following example creates an ascending index on the field
If the The following example creates a compound index on the
Changed
in version 4.4: Starting in MongoDB 4.4, compound indexes can include a single hashed field. Compound hashed indexes require featureCompatibilityVersion set to The following example creates a compound index on the
The order of fields in a compound index is important for supporting TipThe following example creates an index named
The following example creates a compound index named
The collation applies to the indexed keys whose values are string. For queries or sort operations on the indexed keys that uses the same collation rules, MongoDB can use the index. For details, see Collation and Index Use. New in version 4.2. The
For complete documentation on Wildcard Indexes, see Wildcard Indexes. The following lists examples of wildcard index creation:
Consider a
collection
The following operation creates a wildcard index on the
With this wildcard index, MongoDB indexes all scalar values of The wildcard index can support arbitrary single-field queries on
NoteThe path-specific wildcard index syntax is incompatible with the Consider a collection
The following operation creates a wildcard index on all scalar fields (excluding the
With this wildcard index, MongoDB indexes all scalar fields for each document in the collection. If a given field is a nested document or array, the wildcard index recurses into the document/array and indexes all scalar fields in the document/array. The created index can support queries on any arbitrary field within documents in the collection:
NoteWildcard indexes omit the Consider a collection
The following operation creates a wildcard index
and uses the
While the key pattern If a field is a nested document or array, the wildcard index recurses into the document/array and indexes all scalar fields in the document/array. The created index can support queries on any scalar field included in the
NoteWildcard indexes do not support mixing inclusion and exclusion statements in the Consider a collection
The following operation creates a
wildcard index and uses the
While the key pattern If a field is a nested document or array, the wildcard index recurses into the document/array and indexes all scalar fields in the document/array. The created index can support queries on any scalar field except those excluded by
NoteWildcard indexes do not support mixing inclusion and exclusion statements in the NoteRequires featureCompatibilityVersion 4.4+Each MongoDB 4.4 running Starting with MongoDB 4.4, index builds on a replica set or sharded cluster build simultaneously across all data-bearing replica set members. For sharded clusters, the index build occurs only on shards containing data for the collection being indexed. The primary requires a minimum number of data-bearing
Specify the
commitQuorum parameter to the The following operation creates an index with a commit quorum of
The primary marks index build as ready only after a simple majority of data-bearing voting members "vote" to commit the index build. For more information on index builds and the voting process, see Index Builds in Replicated Environments.
Can we create index in MongoDB?MongoDB creates a unique index on the _id field during the creation of a collection. The _id index prevents clients from inserting two documents with the same value for the _id field. You cannot drop this index on the _id field.
How do I index a field in MongoDB?MongoDB provides complete support for indexes on any field in a collection of documents. By default, all collections have an index on the _id field, and applications and users may add additional indexes to support important queries and operations. This document describes ascending/descending indexes on a single field.
Which command will create a single field index with a custom name?The createIndex method is used to create an index based on the “Employeeid” of the document. The '1' parameter indicates that when the index is created with the “Employeeid” Field values, they should be sorted in ascending order.
What is the correct syntax for creating index in MongoDB?Syntax. The basic syntax of createIndex() method is as follows(). Here key is the name of the field on which you want to create index and 1 is for ascending order. To create index in descending order you need to use -1.
|