Docs Menu
Docs Home
/
MongoDB Manual
/ / /

planCacheSetFilter

On this page

  • Definition
  • Syntax
  • Command Fields
  • Required Access
  • Examples
planCacheSetFilter

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>,
collation: { <collation> },
indexes: [ <index1>, <index2>, ...],
comment: <any>
}
)

The query shape for the index filter is the combination of:

  • query

  • sort

  • projection

  • collation

The command has the following fields:

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

The query predicate for the index filter.

Only the predicate structure, including the field names, is used in the index filter. The field values in the query predicate are not used. Therefore, the query predicate in an index filter is used by similar queries that differ only in the field values.

sort
document
Optional. The sort for the index filter.
projection
document
Optional. The projection for the index filter.
collation
document

Specifies the collation to use for the operation.

Collation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks.

The collation option has the following syntax:

collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}

When specifying collation, the locale field is mandatory; all other collation fields are optional. For descriptions of the fields, see Collation Document.

If the collation is unspecified but the collection has a default collation (see db.createCollection()), the operation uses the collation specified for the collection.

If no collation is specified for the collection or for the operations, MongoDB uses the simple binary comparison used in prior versions for string comparisons.

You cannot specify multiple collations for an operation. For example, you cannot specify different collations per field, or if performing a find with a sort, you cannot use one collation for the find and another for the sort.

Starting in MongoDB 6.0, an index filter uses the collation previously set using the planCacheSetFilter command.

indexes
array

An array of index filters for the specified query shape.

Specify the index filters as one of these arrays:

  • Index specification documents. For example, [ { x : 1 }, ... ].

  • Index names. For example, [ "x_1", ... ].

The query optimizer uses either a collection scan or the index arrays for the query plan. If the specified indexes do not exist or are hidden, the optimizer uses a collection scan.

For multiple indexes with the same key pattern, you must specify the index as an array of names.

comment
any

Optional. A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:

A comment can be any valid BSON type (string, integer, object, array, etc).

New in version 4.4.

Index filters only exist for the duration of the server process and do not persist after shutdown. To clear the index filters, use the planCacheClearFilters command.

A user must have access that includes the planCacheIndexFilter action.

The following example creates an index filter on the orders collection such that for queries that consist 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 will apply an index filter for a query shape, check the indexFilterSet field of either the db.collection.explain() or the cursor.explain() method.

The following example creates an index filter for the orders collection. The filter applies to queries whose predicate is an equality match on the item field, where only the quantity field is projected and an ascending sort by order_date is specified.

db.runCommand(
{
planCacheSetFilter: "orders",
query: { item: "ABC" },
projection: { quantity: 1, _id: 0 },
sort: { order_date: 1 },
indexes: [
{ item: 1, order_date: 1 , quantity: 1 }
]
}
)

For the query shape, the query optimizer will only consider indexed plans which use the index { item: 1, order_date: 1, quantity: 1 }.

The following example creates an index filter for the orders collection. The filter applies to queries whose predicate is an equality match on the item field and the collation en_US (English United States).

db.runCommand(
{
planCacheSetFilter: "orders",
query: { item: "Movie" },
collation: { locale: "en_US" },
indexes: [
{ item: 1, order_date: 1 , quantity: 1 }
]
}
)

For the query shape, the query optimizer only uses indexed plans that use the index { item: 1, order_date: 1, quantity: 1 }.

Starting in MongoDB 6.0, an index filter uses the collation previously set using the planCacheSetFilter command.

Tip

See also:

Back

planCacheListFilters