OPTIONS

planCacheSetFilter

Definition

planCacheSetFilter

New in version 2.6.

Set an index filter for a collection. If an index filter already exists for the query shape, the command overrides the previous index filter.

The command has the following syntax:

db.runCommand(
   {
      planCacheSetFilter: <collection>,
      query: <query>,
      sort: <sort>,
      projection: <projection>,
      indexes: [ <index1>, <index2>, ...]
   }
)

The planCacheSetFilter command has the following field:

Field Type Description
planCacheSetFilter string The name of the collection.
query document

The query predicate associated with the index filter. Together with the sort and the projection, the query predicate make up the query shape for the specified index filter.

Only the structure of the predicate, including the field names, are significant; the values in the query predicate are insignificant. As such, query predicates cover similar queries that differ only in the values.

sort document Optional. The sort associated with the filter. Together with the query and the projection, the sort make up the query shape for the specified index filter.
projection document Optional. The projection associated with the filter. Together with the query and the sort, the projection make up the query shape for the specified index filter.
indexes array An array of index specification documents that act as the index filter for the specified query shape. Because the query optimizer chooses among the collection scan and these indexes, if the indexes are non-existent, the optimizer will choose the collection scan.

Index filters only exist for the duration of the server process and do not persist after shutdown; however, you can also clear existing index filters using the planCacheClearFilters command.

Required Access

A user must have access that includes the planCacheIndexFilter action.

Examples

The following example creates an index filter on the orders collection such that for queries that consists only of an equality match on the status field without any projection and sort, the query optimizer evaluates only the two specified indexes and the collection scan for the winning plan:

db.runCommand(
   {
      planCacheSetFilter: "orders",
      query: { status: "A" },
      indexes: [
         { cust_id: 1, status: 1 },
         { status: 1, order_date: -1 }
      ]
   }
)

In the query predicate, only the structure of the predicate, including the field names, are significant; the values are insignificant. As such, the created filter applies to the following operations:

db.orders.find( { status: "D" } )
db.orders.find( { status: "P" } )

To see whether MongoDB applied an index filter for a query, check the explain.filterSet field of the explain() output.