Show
The unique partial indexes feature allows you more flexibility to specify exactly which fields in which documents you’d like to index, all while enforcing uniqueness of that field’s value. This feature is used by specifying a partialFilterExpression when creating the index along with a 'unique' constraint in your Azure Cosmos DB API for MongoDB index. This results in the unique constraint being applied only to the documents that meet the specified filter expression. Learn more. The db.collection.createIndex() method is used to builds an index on a collection. Table of Contents Syntax: db.collection.createIndex(keys, options) Parameters:
Options The following options are available for all index types unless otherwise specified:
Sample document in the restaurants collection:
Example: Create an Ascending Index on a Single FieldThe following example creates an ascending index on the field cuisine.
Example: Create an Index on a Multiple FieldsThe following example creates a compound index on the cuisine field (in ascending order) and the zipcode field (in descending order.)
Retrieve the restaurants data from here BehaviorThe createIndex() method has the behaviors described here.
Previous: db.collection.count() method Can we create multiple index on same collection in MongoDB?Multiple index options can be specified in the same document. However, if you specify multiple option documents the db. collection. createIndexes() operation will fail. How do I create a unique compound index in MongoDB?To create a unique index, use the db. collection. createIndex() method with the unique option set to true . Can I create an unique index?You cannot create a unique index on a single column if that column contains NULL in more than one row. Similarly, you cannot create a unique index on multiple columns if the combination of columns contains NULL in more than one row. These are treated as duplicate values for indexing purposes. How do I change an existing index in MongoDB?To modify an existing index in the MongoDB Shell, you need to drop and recreate the index. The exception to this rule is TTL indexes, which can be modified via the collMod command in conjunction with the index collection flag. |