> This page location: /docs.md > /docs/development.md > /docs/manual/reference/method.md

> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# PlanCache.list() (mongosh method)

## Definition

Returns an array of [plan cache](https://www.mongodb.com/docs/manual/core/query-plans.md#std-label-query-plans-query-optimization) entries for a collection.

The method is only available from the [`plan cache object`](https://www.mongodb.com/docs/manual/reference/method/db.collection.getplancache.md#mongodb-method-db.collection.getPlanCache) of a specific collection; i.e.

```javascript
db.collection.getPlanCache().list(<pipeline>)
```

| Parameter | Type | Description |
| --- | --- | --- |
| [pipeline](https://www.mongodb.com/docs/manual/reference/method/plancache.list.md#std-label-plancache-list-pipeline) | Array | Optional. [Aggregation pipeline](https://www.mongodb.com/docs/manual/core/aggregation-pipeline.md#std-label-aggregation-pipeline) to filter/process the [plan cache query shapes.](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-plan-cache-query-shape) |

You can run this method from a [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) or [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) instance. When run on a sharded cluster, the method returns plan cache entry information from a single member in each shard replica set. This member is identified with the [shard](https://www.mongodb.com/docs/manual/reference/operator/aggregation/plancachestats.md#std-label-plancachestats-shard) and [host](https://www.mongodb.com/docs/manual/reference/operator/aggregation/plancachestats.md#std-label-plancachestats-host) fields. See also [Read Preference.](https://www.mongodb.com/docs/manual/reference/method/plancache.list.md#std-label-plancache-list-read-pref)

The method [`PlanCache.list()`](https://www.mongodb.com/docs/manual/reference/method/plancache.list.md#mongodb-method-PlanCache.list) wraps the [`$planCacheStats`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/plancachestats.md#mongodb-pipeline-pipe.-planCacheStats) aggregation pipeline. That is,

```javascript
db.collection.getPlanCache().list([<stage1>, <stage2>, ...] )
```

is equivalent to

```javascript
db.collection.aggregate([ <$planCacheStats stage>, <stage1>, <stage2>, ... ]).toArray();
```

For details on the output, see [$planCacheStats output.](https://www.mongodb.com/docs/manual/reference/operator/aggregation/plancachestats.md#std-label-planCacheStats-output)

Not all queries automatically place a query plan in the cache. [`PlanCache.list()`](https://www.mongodb.com/docs/manual/reference/method/plancache.list.md#mongodb-method-PlanCache.list) returns an empty array if there are currently no plan cache query shapes with cached query plans.

**See also:**

- [`$planCacheStats`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/plancachestats.md#mongodb-pipeline-pipe.-planCacheStats)

- [`db.collection.aggregate()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.aggregate.md#mongodb-method-db.collection.aggregate)

- [`toArray()`](https://www.mongodb.com/docs/manual/reference/method/cursor.toarray.md#mongodb-method-cursor.toArray)

## Compatibility

This method is available in deployments hosted in the following environments:

- [MongoDB Atlas](https://www.mongodb.com/docs/atlas): The fully managed service for MongoDB deployments in the cloud

* [MongoDB Enterprise](https://www.mongodb.com/docs/manual/administration/install-enterprise.md#std-label-install-mdb-enterprise): The subscription-based, self-managed version of MongoDB

* [MongoDB Community](https://www.mongodb.com/docs/manual/administration/install-community.md#std-label-install-mdb-community-edition): The source-available, free-to-use, and self-managed version of MongoDB

## Considerations

### Query Settings

Starting in MongoDB 8.0, use query settings instead of adding [index filters](https://www.mongodb.com/docs/manual/core/query-plans.md#std-label-index-filters). Index filters are deprecated starting in MongoDB 8.0.

Query settings have more functionality than index filters. Also, index filters aren't persistent and you cannot easily create index filters for all cluster nodes. To add query settings and explore examples, see [`setQuerySettings`.](https://www.mongodb.com/docs/manual/reference/command/setquerysettings.md#mongodb-dbcommand-dbcmd.setQuerySettings)

### Restrictions

[`PlanCache.list()`](https://www.mongodb.com/docs/manual/reference/method/plancache.list.md#mongodb-method-PlanCache.list) is not allowed in [transactions.](https://www.mongodb.com/docs/manual/core/transactions.md)

### Query Hash and Plan Cache Query Shapes

To help identify slow queries with the same [plan cache query shape](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-plan-cache-query-shape), each plan cache query shape is associated with a query hash. The plan cache query shape hash is a hexadecimal string that represents a hash of the query shape and is dependent only on the query shape.

**Note:**

As with any hash function, two different query shapes may result in the same hash value. However, the occurrence of hash collisions between different query shapes is unlikely.

Starting in MongoDB 8.0, the existing `queryHash` field is duplicated in a new field named `planCacheShapeHash`. If you're using an earlier MongoDB version, you'll only see the `queryHash` field. Future MongoDB versions will remove the deprecated `queryHash` field, and you'll need to use the `planCacheShapeHash` field instead.

The query optimizer only caches the plan for a [plan cache query shape](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-plan-cache-query-shape) that can have more than one viable plan.

Each entry in the plan cache is associated with a `planCacheShapeHash`.

**Warning:**

Starting in MongoDB 8.0, the existing `queryHash` field is duplicated in a new field named `planCacheShapeHash`. If you're using an earlier MongoDB version, you'll only see the `queryHash` field. Future MongoDB versions will remove the deprecated `queryHash` field, and you'll need to use the `planCacheShapeHash` field instead.

### Read Preference

[`PlanCache.list()`](https://www.mongodb.com/docs/manual/reference/method/plancache.list.md#mongodb-method-PlanCache.list) observes the [read preference](https://www.mongodb.com/docs/manual/core/read-preference.md) in selecting the host(s) from which to return the plan cache information.

Applications may target different members of a replica set. As such, each replica set member might receive different read commands and have plan cache information that differs from other members. Nevertheless, running [`PlanCache.list()`](https://www.mongodb.com/docs/manual/reference/method/plancache.list.md#mongodb-method-PlanCache.list) on a replica set or a sharded cluster obeys the normal read preference rules. That is, on a replica set, the operation gathers plan cache information from just one member of replica set, and on a sharded cluster, the operation gathers plan cache information from just one member of each shard replica set.

## Required Access

On systems running with [`authorization`](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-security.authorization), the user must have the [`planCacheRead`](https://www.mongodb.com/docs/manual/reference/privilege-actions.md#mongodb-authaction-planCacheRead) privilege for the collection.

## Examples

**Note:**

- Not all queries automatically place a query plan in the cache. [`PlanCache.list()`](https://www.mongodb.com/docs/manual/reference/method/plancache.list.md#mongodb-method-PlanCache.list) returns an empty array if there are currently no query shapes with cached query plans.

- For details on the output, see [$planCacheStats output.](https://www.mongodb.com/docs/manual/reference/operator/aggregation/plancachestats.md#std-label-planCacheStats-output)

The examples in this section use the following `orders` collection:

```javascript
db.orders.insertMany( [
   { "_id" : 1, "item" : "abc", "price" : Decimal128("12"), "quantity" : 2, "type": "apparel" },
   { "_id" : 2, "item" : "jkl", "price" : Decimal128("20"), "quantity" : 1, "type": "electronics" },
   { "_id" : 3, "item" : "abc", "price" : Decimal128("10"), "quantity" : 5, "type": "apparel" },
   { "_id" : 4, "item" : "abc", "price" : Decimal128("8"), "quantity" : 10, "type": "apparel" },
   { "_id" : 5, "item" : "jkl", "price" : Decimal128("15"), "quantity" : 15, "type": "electronics" }
] )
```

Create the following indexes on the collection:

```javascript
db.orders.createIndex( { item: 1 } );
db.orders.createIndex( { item: 1, quantity: 1 } );
db.orders.createIndex( { item: 1, price: 1 }, { partialFilterExpression: { price: { $gte: Decimal128("10")} } } );
db.orders.createIndex( { quantity: 1 } );
db.orders.createIndex( { quantity: 1, type: 1 } );
```

**Note:**

Index `{ item: 1, price: 1 }` is a [partial index](https://www.mongodb.com/docs/manual/core/index-partial.md) and only indexes documents with `price` field greater than or equal to `Decimal128("10")`.

Run some queries against the collection:

```javascript
db.orders.find( { item: "abc", price: { $gte: Decimal128("10") } } )
db.orders.find( { item: "abc", price: { $gte: Decimal128("5") } } )
db.orders.find( { quantity: { $gte: 20 } } )
db.orders.find( { quantity: { $gte: 5 }, type: "apparel" } )
```

### Return Information for All Entries in the Query Cache

The following returns the [plan cache query shapes](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-plan-cache-query-shape) that have cached plans for the `orders` collection:

```javascript
db.orders.getPlanCache().list()
```

The method returns an array of the plan cache query shapes currently in the cache. In the example, the `orders` collection had cached query plans associated with the following shapes:

```javascript
[
   {
      "createdFromQuery" : {
         "query" : { "quantity" : { "$gte" : 5 }, "type" : "apparel" },
         "sort" : { },
         "projection" : { }
      },
      "planCacheShapeHash" : "4D151C4C",
      "planCacheKey" : "DD67E353",
      "isActive" : false,
      "works" : Long(4),
      "cachedPlan" : {
         "stage" : "FETCH",
         "filter" : { "type" : { "$eq" : "apparel" } },
         "inputStage" : {
            "stage" : "IXSCAN",
            "keyPattern" : { "quantity" : 1 },
            "indexName" : "quantity_1",
            "isMultiKey" : false,
            "multiKeyPaths" : { "quantity" : [ ] },
            "isUnique" : false,
            "isSparse" : false,
            "isPartial" : false,
            "indexVersion" : 2,
            "direction" : "forward",
            "indexBounds" : { "quantity" : [ "[5.0, inf.0]" ] }
         }
      },
      "timeOfCreation" : ISODate("2020-02-06T15:57:18.219Z"),
      "creationExecStats" : [
         {
            "nReturned" : 2,
            "executionTimeMillisEstimate" : 0,
            "totalKeysExamined" : 3,
            "totalDocsExamined" : 3,
            "executionStages" : {
               "stage" : "FETCH",
               "filter" : { "type" : { "$eq" : "apparel" } },
               "nReturned" : 2,
               "executionTimeMillisEstimate" : 0,
               "works" : 4,
               ...
            }
         },
         {
            "nReturned" : 2,
            "executionTimeMillisEstimate" : 0,
            "peakTrackedMemBytes" : 795,  // Available starting in MongoDB 8.3
            "totalKeysExamined" : 3,
            "totalDocsExamined" : 2,
            "executionStages" : {
               "stage" : "FETCH",
               "nReturned" : 2,
               "executionTimeMillisEstimate" : 0,
               "works" : 4,
               ...
            }
         }
      ],
      "candidatePlanScores" : [
         1.5002,
         1.5002
      ],
      "indexFilterSet" : false,
      "estimatedSizeBytes" : Long(3160),   // Available starting in MongoDB 5.0
      "host" : "mongodb1.example.net:27018",
      "shard" : "shardA"                         // Available if run on sharded cluster
   },

   {
      "createdFromQuery" : {
         "query" : { "quantity" : { "$gte" : 20 } },
         "sort" : { },
         "projection" : { }
      },
      "planCacheShapeHash" : "23B19B75",
      "planCacheKey" : "6F23F858",
      "isActive" : false,
      "works" : Long(1),
      ...
   },
   {
      "createdFromQuery" : {
         "query" : { "item" : "abc", "price" : { "$gte" : Decimal128("5") } },
         "sort" : { },
         "projection" : { }
      },
      "planCacheShapeHash" : "117A6B10",
      "planCacheKey" : "A1824628",
      "isActive" : false,
      "works" : Long(4),
      ...
   },
   {
      "createdFromQuery" : {
         "query" : { "item" : "abc", "price" : { "$gte" : Decimal128("10") } },
         "sort" : { },
         "projection" : { }
      },
      "planCacheShapeHash" : "117A6B10",
      "planCacheKey" : "2E6E536B",
      "isActive" : false,
      "works" : Long(3),
      ...
   }
]
```

**Warning:**

Starting in MongoDB 8.0, the existing `queryHash` field is duplicated in a new field named `planCacheShapeHash`. If you're using an earlier MongoDB version, you'll only see the `queryHash` field. Future MongoDB versions will remove the deprecated `queryHash` field, and you'll need to use the `planCacheShapeHash` field instead.

For details on the output, see [$planCacheStats output.](https://www.mongodb.com/docs/manual/reference/operator/aggregation/plancachestats.md#std-label-planCacheStats-output)

### List Plan Cache Query Shapes

To obtain a list of all of the query shapes for which there is a cached plan, you can use the [`PlanCache.list()`](https://www.mongodb.com/docs/manual/reference/method/plancache.list.md#mongodb-method-PlanCache.list). For example, the following operation passes in a pipeline with a [`$project`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/project.md#mongodb-pipeline-pipe.-project) stage to only output the [createdFromQuery](https://www.mongodb.com/docs/manual/reference/operator/aggregation/plancachestats.md#std-label-plancachestats-createdFromQuery) field and the [planCacheShapeHash](https://www.mongodb.com/docs/manual/reference/operator/aggregation/plancachestats.md#std-label-plancachestats-planCacheShapeHash) field.

```javascript
db.orders.getPlanCache().list( [ { $project: {createdFromQuery: 1, planCacheShapeHash: 1 } } ] )
```

**Warning:**

Starting in MongoDB 8.0, the existing `queryHash` field is duplicated in a new field named `planCacheShapeHash`. If you're using an earlier MongoDB version, you'll only see the `queryHash` field. Future MongoDB versions will remove the deprecated `queryHash` field, and you'll need to use the `planCacheShapeHash` field instead.

The operation returns the following plan cache query shapes:

```javascript
[
   { "createdFromQuery" : { "query" : { "quantity" : { "$gte" : 5 }, "type" : "apparel" }, "sort" : { }, "projection" : { } }, "planCacheShapeHash" : "4D151C4C" },
   { "createdFromQuery" : { "query" : { "quantity" : { "$gte" : 20 } }, "sort" : { }, "projection" : { } }, "planCacheShapeHash" : "23B19B75" },
   { "createdFromQuery" : { "query" : { "item" : "abc", "price" : { "$gte" : Decimal128("5") } }, "sort" : { }, "projection" : { } }, "planCacheShapeHash" : "117A6B10" },
   { "createdFromQuery" : { "query" : { "item" : "abc", "price" : { "$gte" : Decimal128("10") } }, "sort" : { }, "projection" : { } }, "planCacheShapeHash" : "117A6B10" }
]
```

For details on the output, see [$planCacheStats output.](https://www.mongodb.com/docs/manual/reference/operator/aggregation/plancachestats.md#std-label-planCacheStats-output)

### Find Cache Entry Details for a Plan Cache Query Shape

To return plan cache information for a particular query shape, pass in a pipeline with a [`$match`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/match.md#mongodb-pipeline-pipe.-match) on the [planCacheKey](https://www.mongodb.com/docs/manual/reference/operator/aggregation/plancachestats.md#std-label-plancachestats-planCacheKey) field.

```javascript
db.orders.getPlanCache().list([ { $match: { planCacheKey: "DD67E353"} } ] )
```

The operation returns the following:

```javascript
[
   {
      "createdFromQuery" : {
         "query" : {
            "quantity" : {
               "$gte" : 5
            },
            "type" : "apparel"
         },
         "sort" : {

         },
         "projection" : {

         }
      },
      "planCacheShapeHash" : "4D151C4C",
      "planCacheKey" : "DD67E353",
      "isActive" : false,
      "works" : Long(4),
      "cachedPlan" : {
         "stage" : "FETCH",
         "filter" : {
            "type" : {
               "$eq" : "apparel"
            }
         },
         "inputStage" : {
            "stage" : "IXSCAN",
            "keyPattern" : {
               "quantity" : 1
            },
            "indexName" : "quantity_1",
            "isMultiKey" : false,
            "multiKeyPaths" : {
               "quantity" : [ ]
            },
            "isUnique" : false,
            "isSparse" : false,
            "isPartial" : false,
            "indexVersion" : 2,
            "direction" : "forward",
            "indexBounds" : {
               "quantity" : [
                  "[5.0, inf.0]"
               ]
            }
         }
      },
      "timeOfCreation" : ISODate("2020-02-11T17:14:33.873Z"),
      "creationExecStats" : [
         {
            "nReturned" : 2,
            "executionTimeMillisEstimate" : 0,
            "peakTrackedMemBytes" : 795,  // Available starting in MongoDB 8.3
            "totalKeysExamined" : 3,
            "totalDocsExamined" : 3,
            "executionStages" : {
               "stage" : "FETCH",
               "filter" : {
                  "type" : {
                     "$eq" : "apparel"
                  }
               },
               "nReturned" : 2,
               "executionTimeMillisEstimate" : 0,
               "works" : 4,
               "advanced" : 2,
               "needTime" : 1,
               "needYield" : 0,
               "saveState" : 0,
               "restoreState" : 0,
               "isEOF" : 1,
               "docsExamined" : 3,
               "alreadyHasObj" : 0,
               "inputStage" : {
                  "stage" : "IXSCAN",
                  "nReturned" : 3,
                  "executionTimeMillisEstimate" : 0,
                  "works" : 4,
                  "advanced" : 3,
                  "needTime" : 0,
                  "needYield" : 0,
                  "saveState" : 0,
                  "restoreState" : 0,
                  "isEOF" : 1,
                  "keyPattern" : {
                     "quantity" : 1
                  },
                  "indexName" : "quantity_1",
                  "isMultiKey" : false,
                  "multiKeyPaths" : {
                     "quantity" : [ ]
                  },
                  "isUnique" : false,
                  "isSparse" : false,
                  "isPartial" : false,
                  "indexVersion" : 2,
                  "direction" : "forward",
                  "indexBounds" : {
                     "quantity" : [
                        "[5.0, inf.0]"
                     ]
                  },
                  "keysExamined" : 3,
                  "seeks" : 1,
                  "dupsTested" : 0,
                  "dupsDropped" : 0
               }
            }
         },
         {
            "nReturned" : 2,
            "executionTimeMillisEstimate" : 0,
            "totalKeysExamined" : 3,
            "totalDocsExamined" : 2,
            "executionStages" : {
               "stage" : "FETCH",
               "nReturned" : 2,
               "executionTimeMillisEstimate" : 0,
               "works" : 4,
               "advanced" : 2,
               "needTime" : 1,
               "needYield" : 0,
               "saveState" : 0,
               "restoreState" : 0,
               "isEOF" : 1,
               "docsExamined" : 2,
               "alreadyHasObj" : 0,
               "inputStage" : {
                  "stage" : "IXSCAN",
                  "nReturned" : 2,
                  "executionTimeMillisEstimate" : 0,
                  "peakTrackedMemBytes" : 795,  // Available starting in MongoDB 8.3
                  "works" : 4,
                  "advanced" : 2,
                  "needTime" : 1,
                  "needYield" : 0,
                  "saveState" : 0,
                  "restoreState" : 0,
                  "isEOF" : 1,
                  "keyPattern" : {
                     "quantity" : 1,
                     "type" : 1
                  },
                  "indexName" : "quantity_1_type_1",
                  "isMultiKey" : false,
                  "multiKeyPaths" : {
                     "quantity" : [ ],
                     "type" : [ ]
                  },
                  "isUnique" : false,
                  "isSparse" : false,
                  "isPartial" : false,
                  "indexVersion" : 2,
                  "direction" : "forward",
                  "indexBounds" : {
                     "quantity" : [
                        "[5.0, inf.0]"
                     ],
                     "type" : [
                        "[\"apparel\", \"apparel\"]"
                     ]
                  },
                  "keysExamined" : 3,
                  "seeks" : 2,
                  "dupsTested" : 0,
                  "dupsDropped" : 0
               }
            }
         }
      ],
      "candidatePlanScores" : [
         1.5002,
         1.5002
      ],
      "indexFilterSet" : false,
      "estimatedSizeBytes" : Long(3160),   // Available starting in MongoDB 5.0
      "host" : "mongodb1.example.net:27018",
      "shard" : "shardA"                         // Available if run on sharded cluster
   }
]
```

**Warning:**

Starting in MongoDB 8.0, the existing `queryHash` field is duplicated in a new field named `planCacheShapeHash`. If you're using an earlier MongoDB version, you'll only see the `queryHash` field. Future MongoDB versions will remove the deprecated `queryHash` field, and you'll need to use the `planCacheShapeHash` field instead.

For details on the output, see [$planCacheStats output.](https://www.mongodb.com/docs/manual/reference/operator/aggregation/plancachestats.md#std-label-planCacheStats-output)
