Updated on 2022-06-21 GMT+08:00

Query

To avoid slow queries, analyze and optimize the execution process (query plan).

db.collection.find().explain()

For details, see Performance and official documents.

Precaution

  • The returned query result is a cursor. After using the cursor, close it in time. Otherwise, memory leaks accumulate.
  • Create necessary indexes based on search criteria. For details about index design, see Index.
    • Do not use COLLSCAN to scan the entire table.
    • Query conditions and index fields are matched in sequence.
  • For cluster instances, shard tables properly based on service requirements. For details about sharding, see Sharding.
  • For sharded tables, query conditions must be based on shard keys to avoid unnecessary queries such as scatter-gather. For details, see Distributed Queries.
  • You can specify a readConcern level for queries. For details, see Read Concern.
  • You can specify the readPerference parameter for queries. For details, see Read Preference.