Create Indexes to Support Queries
For commonly issued queries, create indexes. If a query searches multiple fields, create a compound index. Scanning an index is much faster than scanning a collection. The indexes structures are smaller than the documents reference, and store references in order.
Example
If you have a posts collection containing blog posts, and if you regularly issue a query that sorts on the author_name field, then you can optimize the query by creating an index on the author_name field:
db.posts.createIndex( { author_name : 1 } )
复制代码
Indexes also improve efficiency on queries that routinely sort on a given field.
Example
If you regularly issue a query that sorts on the timestamp field, then you can optimize the query by creating an index on the timestamp field:
Creating this index:
db.posts.createIndex( { timestamp : 1 } )
复制代码
Optimizes this query:
db.posts.find().sort( { timestamp : -1 } )
复制代码
Because MongoDB can read indexes in both ascending and descending order, the direction of a single-key index does not matter.
Indexes support queries, update operations, and some phases of the aggregation pipeline.
Index keys that are of the BinData type are more efficiently stored in the index if:
- the binary subtype value is in the range of 0-7 or 128-135, and
- the length of the byte array is: 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, or 32.
本文使用 文章同步助手 同步






















![[桜井宁宁]COS和泉纱雾超可爱写真福利集-一一网](https://www.proyy.com/skycj/data/images/2020-12-13/4d3cf227a85d7e79f5d6b4efb6bde3e8.jpg)

![[桜井宁宁] 爆乳奶牛少女cos写真-一一网](https://www.proyy.com/skycj/data/images/2020-12-13/d40483e126fcf567894e89c65eaca655.jpg)