Index types
In the following section, you will explore different index types, understanding how and when to utilize them for optimal outcomes.
Single-field indexes and indexing embedded documents
The simplest and the most common type of index is the single-field index. By default, MongoDB automatically generates single-field indexes for every collection—a unique index on the _id
field. This index prevents the insertion of a second document with the same value for the _id
field into the collection.
In the previous section, you saw an example of the simplest possible single-field index. You can further enhance your books
collection by adding a couple more indexes—one for the titles and one for the price, which is an embedded field, to make it unique.
Execute the following command:
db.books.createIndex({ "title": 1 }, { "unique": true })
Note
Two indexes cannot have the same shape even in cases when the options are different. For...