cursor -- Tools for iterating over MongoDB query results#
Cursor class to iterate over Mongo query results.
- class pymongo.cursor.CursorType#
- NON_TAILABLE#
The standard cursor type.
- TAILABLE#
The tailable cursor type.
Tailable cursors are only for use with capped collections. They are not closed when the last data is retrieved but are kept open and the cursor location marks the final document position. If more data is received iteration of the cursor will continue from the last document received.
- TAILABLE_AWAIT#
A tailable cursor with the await option set.
Creates a tailable cursor that will wait for a few seconds after returning the full result set so that it can capture and return additional data added during the query.
- EXHAUST#
An exhaust cursor.
MongoDB will stream batched results to the client without waiting for the client to request each batch, reducing latency.
- class pymongo.cursor.Cursor(collection, filter=None, projection=None, skip=0, limit=0, no_cursor_timeout=False, cursor_type=CursorType.NON_TAILABLE, sort=None, allow_partial_results=False, oplog_replay=False, batch_size=0, collation=None, hint=None, max_scan=None, max_time_ms=None, max=None, min=None, return_key=False, show_record_id=False, snapshot=False, comment=None, session=None, allow_disk_use=None)#
Create a new cursor.
Should not be called directly by application developers - see
find()instead.参见
The MongoDB documentation on cursors.
- c[index]
See
__getitem__()and read the warning.
- __getitem__(index: int) _DocumentType#
- __getitem__(index: slice) Cursor[_DocumentType]
Get a single document or a slice of documents from this cursor.
警告
A
Cursoris not a Pythonlist. Each index access or slice requires that a new query be run using skip and limit. Do not iterate the cursor using index accesses. The following example is extremely inefficient and may return surprising results:cursor = db.collection.find() # Warning: This runs a new query for each document. # Don't do this! for idx in range(10): print(cursor[idx])
Raises
InvalidOperationif this cursor has already been used.To get a single document use an integral index, e.g.:
>>> db.test.find()[50]
An
IndexErrorwill be raised if the index is negative or greater than the amount of documents in this cursor. Any limit previously applied to this cursor will be ignored.To get a slice of documents use a slice index, e.g.:
>>> db.test.find()[20:25]
This will return this cursor with a limit of
5and skip of20applied. Using a slice index will override any prior limits or skips applied to this cursor (including those applied through previous calls to this method). RaisesIndexErrorwhen the slice has a step, a negative start value, or a stop value less than or equal to the start value.- Parameters:
index: An integer or slice index to be applied to this cursor
- add_option(mask: int) Cursor[_DocumentType]#
Set arbitrary query flags using a bitmask.
To set the tailable flag: cursor.add_option(2)
- property address: Tuple[str, Any] | None#
The (host, port) of the server used, or None.
在 3.0 版本发生变更: Renamed from "conn_id".
- property alive: bool#
Does this cursor have the potential to return more data?
This is mostly useful with tailable cursors since they will stop iterating even though they may return more results in the future.
With regular cursors, simply use a for loop instead of
alive:for doc in collection.find(): print(doc)
- allow_disk_use(allow_disk_use: bool) Cursor[_DocumentType]#
Specifies whether MongoDB can use temporary disk files while processing a blocking sort operation.
Raises
TypeErrorif allow_disk_use is not a boolean.备注
allow_disk_use requires server version >= 4.4
- Parameters:
allow_disk_use: if True, MongoDB may use temporary disk files to store data exceeding the system memory limit while processing a blocking sort operation.
在 3.11 版本加入.
- batch_size(batch_size: int) Cursor[_DocumentType]#
Limits the number of documents returned in one batch. Each batch requires a round trip to the server. It can be adjusted to optimize performance and limit data transfer.
备注
batch_size can not override MongoDB's internal limits on the amount of data it will return to the client in a single batch (i.e if you set batch size to 1,000,000,000, MongoDB will currently only return 4-16MB of results per batch).
Raises
TypeErrorif batch_size is not an integer. RaisesValueErrorif batch_size is less than0. RaisesInvalidOperationif thisCursorhas already been used. The last batch_size applied to this cursor takes precedence.- Parameters:
batch_size: The size of each batch of results requested.
- clone() Cursor[_DocumentType]#
Get a clone of this cursor.
Returns a new Cursor instance with options matching those that have been set on the current instance. The clone will be completely unevaluated, even if the current instance has been partially or completely evaluated.
- collation(collation: _CollationIn | None) Cursor[_DocumentType]#
Adds a
Collationto this query.Raises
TypeErrorif collation is not an instance ofCollationor adict. RaisesInvalidOperationif thisCursorhas already been used. Only the last collation applied to this cursor has any effect.- Parameters:
collation: An instance of
Collation.
- property collection: Collection[_DocumentType]#
The
Collectionthat thisCursoris iterating.
- comment(comment: Any) Cursor[_DocumentType]#
Adds a 'comment' to the cursor.
http://mongodb.com/docs/manual/reference/operator/comment/
- Parameters:
comment: A string to attach to the query to help interpret and trace the operation in the server logs and in profile data.
在 2.7 版本加入.
- distinct(key: str) List#
Get a list of distinct values for key among all documents in the result set of this query.
Raises
TypeErrorif key is not an instance ofstr.The
distinct()method obeys theread_preferenceof theCollectioninstance on whichfind()was called.- Parameters:
key: name of key for which we want to get the distinct values
- explain() _DocumentType#
Returns an explain plan record for this cursor.
备注
This method uses the default verbosity mode of the explain command,
allPlansExecution. To use a different verbosity usecommand()to run the explain command directly.参见
The MongoDB documentation on explain.
- hint(index: str | Sequence[str | Tuple[str, int | str | Mapping[str, Any]]] | None) Cursor[_DocumentType]#
Adds a 'hint', telling Mongo the proper index to use for the query.
Judicious use of hints can greatly improve query performance. When doing a query on multiple fields (at least one of which is indexed) pass the indexed field as a hint to the query. Raises
OperationFailureif the provided hint requires an index that does not exist on this collection, and raisesInvalidOperationif this cursor has already been used.index should be an index as passed to
create_index()(e.g.[('field', ASCENDING)]) or the name of the index. If index isNoneany existing hint for this query is cleared. The last hint applied to this cursor takes precedence over all others.- Parameters:
index: index to hint on (as an index specifier)
- limit(limit: int) Cursor[_DocumentType]#
Limits the number of results to be returned by this cursor.
Raises
TypeErrorif limit is not an integer. RaisesInvalidOperationif thisCursorhas already been used. The last limit applied to this cursor takes precedence. A limit of0is equivalent to no limit.- Parameters:
limit: the number of results to return
参见
The MongoDB documentation on limit.
- max(spec: Sequence[str | Tuple[str, int | str | Mapping[str, Any]]]) Cursor[_DocumentType]#
Adds
maxoperator that specifies upper bound for specific index.When using
max,hint()should also be configured to ensure the query uses the expected index and starting in MongoDB 4.2hint()will be required.- Parameters:
spec: a list of field, limit pairs specifying the exclusive upper bound for all keys of a specific index in order.
在 3.8 版本发生变更: Deprecated cursors that use
maxwithout ahint().在 2.7 版本加入.
- max_await_time_ms(max_await_time_ms: int | None) Cursor[_DocumentType]#
Specifies a time limit for a getMore operation on a
TAILABLE_AWAITcursor. For all other types of cursor max_await_time_ms is ignored.Raises
TypeErrorif max_await_time_ms is not an integer orNone. RaisesInvalidOperationif thisCursorhas already been used.备注
max_await_time_ms requires server version >= 3.2
- Parameters:
max_await_time_ms: the time limit after which the operation is aborted
在 3.2 版本加入.
- max_scan(max_scan: int | None) Cursor[_DocumentType]#
DEPRECATED - Limit the number of documents to scan when performing the query.
Raises
InvalidOperationif this cursor has already been used. Only the lastmax_scan()applied to this cursor has any effect.- Parameters:
max_scan: the maximum number of documents to scan
在 3.7 版本发生变更: Deprecated
max_scan(). Support for this option is deprecated in MongoDB 4.0. Usemax_time_ms()instead to limit server side execution time.
- max_time_ms(max_time_ms: int | None) Cursor[_DocumentType]#
Specifies a time limit for a query operation. If the specified time is exceeded, the operation will be aborted and
ExecutionTimeoutis raised. If max_time_ms isNoneno limit is applied.Raises
TypeErrorif max_time_ms is not an integer orNone. RaisesInvalidOperationif thisCursorhas already been used.- Parameters:
max_time_ms: the time limit after which the operation is aborted
- min(spec: Sequence[str | Tuple[str, int | str | Mapping[str, Any]]]) Cursor[_DocumentType]#
Adds
minoperator that specifies lower bound for specific index.When using
min,hint()should also be configured to ensure the query uses the expected index and starting in MongoDB 4.2hint()will be required.- Parameters:
spec: a list of field, limit pairs specifying the inclusive lower bound for all keys of a specific index in order.
在 3.8 版本发生变更: Deprecated cursors that use
minwithout ahint().在 2.7 版本加入.
- next() _DocumentType#
Advance the cursor.
- remove_option(mask: int) Cursor[_DocumentType]#
Unset arbitrary query flags using a bitmask.
To unset the tailable flag: cursor.remove_option(2)
- rewind() Cursor[_DocumentType]#
Rewind this cursor to its unevaluated state.
Reset this cursor if it has been partially or completely evaluated. Any options that are present on the cursor will remain in effect. Future iterating performed on this cursor will cause new queries to be sent to the server, even if the resultant data has already been retrieved by this cursor.
- property session: ClientSession | None#
The cursor's
ClientSession, or None.在 3.6 版本加入.
- skip(skip: int) Cursor[_DocumentType]#
Skips the first skip results of this cursor.
Raises
TypeErrorif skip is not an integer. RaisesValueErrorif skip is less than0. RaisesInvalidOperationif thisCursorhas already been used. The last skip applied to this cursor takes precedence.- Parameters:
skip: the number of results to skip
- sort(key_or_list: str | Sequence[str | Tuple[str, int | str | Mapping[str, Any]]], direction: int | str | None = None) Cursor[_DocumentType]#
Sorts this cursor's results.
Pass a field name and a direction, either
ASCENDINGorDESCENDING.:for doc in collection.find().sort('field', pymongo.ASCENDING): print(doc)
To sort by multiple fields, pass a list of (key, direction) pairs. If just a name is given,
ASCENDINGwill be inferred:for doc in collection.find().sort([ 'field1', ('field2', pymongo.DESCENDING)]): print(doc)
Text search results can be sorted by relevance:
cursor = db.test.find( {'$text': {'$search': 'some words'}}, {'score': {'$meta': 'textScore'}}) # Sort by 'score' field. cursor.sort([('score', {'$meta': 'textScore'})]) for doc in cursor: print(doc)
For more advanced text search functionality, see MongoDB's Atlas Search.
Raises
InvalidOperationif this cursor has already been used. Only the lastsort()applied to this cursor has any effect.- Parameters:
key_or_list: a single key or a list of (key, direction) pairs specifying the keys to sort on
direction (optional): only used if key_or_list is a single key, if not given
ASCENDINGis assumed
- where(code: str | Code) Cursor[_DocumentType]#
Adds a $where clause to this query.
The code argument must be an instance of
strorCodecontaining a JavaScript expression. This expression will be evaluated for each document scanned. Only those documents for which the expression evaluates to true will be returned as results. The keyword this refers to the object currently being scanned. For example:# Find all documents where field "a" is less than "b" plus "c". for doc in db.test.find().where('this.a < (this.b + this.c)'): print(doc)
Raises
TypeErrorif code is not an instance ofstr. RaisesInvalidOperationif thisCursorhas already been used. Only the last call towhere()applied to aCursorhas any effect.- Parameters:
code: JavaScript expression to use as a filter
- class pymongo.cursor.RawBatchCursor(collection, filter=None, projection=None, skip=0, limit=0, no_cursor_timeout=False, cursor_type=CursorType.NON_TAILABLE, sort=None, allow_partial_results=False, oplog_replay=False, batch_size=0, collation=None, hint=None, max_scan=None, max_time_ms=None, max=None, min=None, return_key=False, show_record_id=False, snapshot=False, comment=None, allow_disk_use=None)#
Create a new cursor / iterator over raw batches of BSON data.
Should not be called directly by application developers - see
find_raw_batches()instead.参见
The MongoDB documentation on cursors.