OPTIONS

cursor.maxScan()

On this page

Definition

cursor.maxScan()

New in version 3.2.

Specifies a maximum number of documents or index keys the query plan will scan. Once the limit is reached, the query terminates execution and returns the current batch of results.

maxScan() has the following syntax:

cursor.maxScan( <maxScan> )

The method cursor.maxScan has the following parameter:

Parameter Type Description
maxScan int, long, or double The maximum number of documents or index keys that the query plan will scan.
Returns:The cursor that maxScan() is attached to with a modified result set based on the maxScan parameter. This allows for additional cursor modifiers to be chained.

Behavior

For collection scans, maxScan is the maximum number of documents scanned before the query results are returned. For index scans, maxScan is the maximum number of index keys examined.

Using a value of 0 is equivalent to not using cursor.maxScan().

Example

Given the following data:

{ _id : 1, ts : 100, status : "OK" },
{ _id : 2, ts : 200, status : "OK" },
{ _id : 3, ts : 300, status : "WARN" },
{ _id : 4, ts : 400, status : "DANGER" },
{ _id : 5, ts : 500, status : "WARN" },
{ _id : 6, ts : 600, status : "OK" },
{ _id : 7, ts : 700, status : "OK" },
{ _id : 8, ts : 800, status : "WARN" },
{ _id : 9, ts : 900, status : "WARN" },
{ _id : 10, ts : 1000, status : "OK" }

Assuming this query were answered with a collection scan, the following limits the number of documents to scan to 5:

db.collection.find ( { "status" : "OK" } ).maxScan(5)

The operation returns:

{ "_id" : 1, "ts" : 100, "status" : "OK" }
{ "_id" : 2, "ts" : 200, "status" : "OK" }

If this query were answered using an index scan on { status : 1 }, the same operation returns the following:

{ "_id" : 1, "ts" : 100, "status" : "OK" }
{ "_id" : 2, "ts" : 200, "status" : "OK" }
{ "_id" : 6, "ts" : 600, "status" : "OK" }
{ "_id" : 7, "ts" : 700, "status" : "OK" }

Was this page helpful?

Yes No

Thank you for your feedback!

We're sorry! You can Report a Problem to help us improve this page.