MongoCursor
public class MongoCursor<T> : CursorProtocol where T : Decodable, T : Encodable
A MongoDB cursor.
Note that the next
method blocks until a result is received or the cursor is exhausted, so methods inherited from
Sequence
that iterate over the entire sequence may block indefinitely when used on tailable cursors (e.g.
reduce
).
It is safe to kill(...)
a MongoCursor
from another thread while it is blocked waiting on results, however.
-
Indicates whether this cursor has the potential to return more data.
This method is mainly useful if this cursor is tailable, since in that case
tryNext
may return more results even after returningnil
.If this cursor is non-tailable, it will always be dead after either
tryNext
returnsnil
or a non-DecodingError
error.This cursor will be dead after
next
returnsnil
or a non-DecodingError
error, regardless of theMongoCursorType
.This cursor may still be alive after
next
ortryNext
returns aDecodingError
.Declaration
Swift
public func isAlive() -> Bool
-
Returns the ID used by the server to track the cursor.
nil
once all results have been fetched from the server.Declaration
Swift
public var id: Int64? { get }
-
Returns a
Result
containing the nextT
in this cursor, an error if one occurred, ornil
if the cursor is exhausted.If this cursor is tailable, this method will continue polling until a non-empty batch is returned from the server or the cursor is closed.
On failure, there error returned is likely one of the following:
MongoError.CommandError
if an error occurs while fetching more results from the server.MongoError.LogicError
if this function is called after the cursor has died.MongoError.LogicError
if this function is called and the session associated with this cursor is inactive.DecodingError
if an error occurs decoding the server’s response.
Declaration
Swift
public func next() -> Result<T, Error>?
Return Value
A
Result<T, Error>?
containing the nextT
in this cursor on success, an error if one occurred, ornil
if the cursor was exhausted. -
Attempt to get the next
T
from the cursor, returningnil
if there are no results.If this cursor is tailable, this method may be called repeatedly while
isAlive
is true to retrieve new data.If this cursor is a tailable await cursor, it will wait for results server side for a maximum of
maxAwaitTimeMS
before returningnil
. This option can be configured via options passed to the method that created this cursor (e.g. themaxAwaitTimeMS
option on theFindOptions
passed tofind
).On failure, there error returned is likely one of the following: -
MongoError.CommandError
if an error occurs while fetching more results from the server. -MongoError.LogicError
if this function is called after the cursor has died. -MongoError.LogicError
if this function is called and the session associated with this cursor is inactive. -DecodingError
if an error occurs decoding the server’s response.Declaration
Swift
public func tryNext() -> Result<T, Error>?
Return Value
A
Result<T, Error>?
containing the nextT
in this cursor on success, an error if one occurred, ornil
if there were no results. -
Kill this cursor.
This method may be called from another thread safely even if this cursor is blocked retrieving results. This is mainly useful for freeing a thread that a cursor is blocking via a long running operation.
This method is automatically called in the
deinit
ofMongoCursor
, so it is not necessary to call it manually.Declaration
Swift
public func kill()