March 2019
Intermediate to advanced
394 pages
11h 4m
English
The most common and simple type of index is the single field index. An example of a single field and key index is the index on ObjectId (_id), which is generated by default in every MongoDB collection. The ObjectId index is also unique, preventing a second document from having the same ObjectId in a collection.
An index on a single field, based on the mongo_book database that we used throughout the previous chapters, is defined like this:
> db.books.createIndex( { price: 1 } )
Here, we create an index on the field name in ascending order of index creation. For a descending order, the same index would be created like this:
> db.books.createIndex( { price: -1 } )
The ordering for index creation is important if we expect ...
Read now
Unlock full access