MongoDB Query Plan Cache Commands

MongoDB planCacheClear command

The planCacheClear command is used to remove the cached query plans for a collection. It declares the query shape to remove the cached query plan for that shape.

Syntax:

Command Fields

FieldTypeDescription
querydocumentIt contains the prefix for the shape query.
projectiondocumentIt contains the projection details associated with the query shape.
sortdocumentIt contains the sorting details associated with the query shape.

Example:

We have the following query shape:

The following code clears the query plan cached for the shape:

MongoDB planCacheClearFilters command

The planCacheClearFilters command removes index filters on a collection. We can also clear existing index filters with this command while index filters only exist for the duration of the server process and don't persist after shutdown.

Syntax:

Command Fields:

FieldTypeDescription
planCache
ClearFilters
stringIt declares the name of the collection.
querydocumentIt contains the query predicate that is associated with the filter to remove.
sortdocumentIt contains the sorting criteria associated with the filter to remove.
projectiondocumentIt contains the projection details that are associated with the filter to remove.

Example:

The books collection contains the following two filters:

The following command will remove the first index filter:

MongoDB planCacheListFilters command

The palncacheListFilters command lists the index filters associated with query shapes for a collection.

Syntax:

Command Field:

FieldTypeDescription
planCacheListFiltersstringIt declares the name of the collection

Example:

MongoDB palnCacheSetFilter command

This command set an index filter for a collection. It is the index filter that already exists for the query shape. The planCacheSetFilter overwrites the previous index filter.

Syntax:

Command Filters

FieldTypeDescription
planCacheSetFilterstringThis filed declares the name of the collection.
querydocumentIt contains the query predicate associated with the index filter.
sortdocumentIt contains the sorting details associated with the filter.
projectiondocumentIt contains the projection associated with the filter.
indexes1arrayIt contains the array of the index for the specified query shape.

Example:

The example creates an index filter on the books collection such that for queries that contains only of an equality match on the purchase field without any projection and sort.






Latest Courses