MongoCursor
public class MongoCursor<T> : CursorProtocol where T : Decodable, T : Encodable
extension MongoCursor: AsyncSequence, AsyncIteratorProtocol
A MongoDB cursor.
-
The ID used by the server to track the cursor over time. If all of the cursor’s results were returnable in a single batch, or if the cursor contained no results, this value will be nil.
Declaration
Swift
public let id: Int64? -
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
tryNextmay return more results even after returningnil.If this cursor is non-tailable, it will always be dead after either
tryNextreturnsnilor a non-DecodingErrorerror.This cursor will be dead after
nextreturnsnilor a non-DecodingErrorerror, regardless of theMongoCursorType.This cursor may still be alive after
nextortryNextreturns aDecodingError.Warning
If this cursor is alive when it goes out of scope, it will leak resources. To ensure it is dead before it leaves scope, invokeMongoCursor.kill(...)on it.Declaration
Swift
public func isAlive() -> EventLoopFuture<Bool> -
Attempt to get the next
Tfrom the cursor, returningnilif there are no results.If this cursor is tailable, this method may be called repeatedly while
isAliveis true to retrieve new data.If this cursor is a tailable await cursor, it will wait for results server side for a maximum of
maxAwaitTimeMSbefore evaluating tonil. This option can be configured via options passed to the method that created this cursor (e.g. themaxAwaitTimeMSoption on theFindOptionspassed tofind).Note: You must not call any cursor methods besides
killandisAlivewhile the future returned from this method is unresolved. Doing so will result in undefined behavior.Declaration
Swift
public func tryNext() -> EventLoopFuture<T?>Return Value
An
EventLoopFuture<T?>containing the nextTin this cursor, an error if one occurred, ornilif there was no data.If the future evaluates to an error, it is likely one of the following:
MongoError.CommandErrorif an error occurs while fetching more results from the server.MongoError.LogicErrorif this function is called after the cursor has died.MongoError.LogicErrorif this function is called and the session associated with this cursor is inactive.MongoError.LogicErrorif this cursor’s parent client has already been closed.DecodingErrorif an error occurs decoding the server’s response.
-
Get the next
Tfrom the cursor.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.
A thread from the driver’s internal thread pool will be occupied until the returned future is completed, so performance degradation is possible if the number of polling cursors is too close to the total number of threads in the thread pool. To configure the total number of threads in the pool, set the
MongoClientOptions.threadPoolSizeoption during client creation.Note: You must not call any cursor methods besides
killandisAlivewhile the future returned from this method is unresolved. Doing so will result in undefined behavior.If the future fails, the error is likely one of the following: -
MongoError.CommandErrorif an error occurs while fetching more results from the server. -MongoError.LogicErrorif this function is called after the cursor has died. -MongoError.LogicErrorif this function is called and the session associated with this cursor is inactive. -DecodingErrorif an error occurs decoding the server’s response.Declaration
Swift
public func next() -> EventLoopFuture<T?>Return Value
An
EventLoopFuture<T?>evaluating to the nextTin this cursor, ornilif the cursor is exhausted. If the underlying cursor is tailable, the future will not resolve until data is returned (potentially after multiple requests to the server), the cursor is closed, or an error occurs. -
Consolidate the currently available results of the cursor into an array of type
T.If this cursor is not tailable, this method will exhaust it.
If this cursor is tailable,
toArraywill only fetch the currently available results, and it may return more data if it is called again while the cursor is still alive.Note: You must not call any cursor methods besides
killandisAlivewhile the future returned from this method is unresolved. Doing so will result in undefined behavior.Declaration
Swift
public func toArray() -> EventLoopFuture<[T]>Return Value
An
EventLoopFuture<[T]>evaluating to the results currently available in this cursor, or an error.If the future evaluates to an error, that error is likely one of the following:
MongoError.CommandErrorif an error occurs while fetching more results from the server.MongoError.LogicErrorif this function is called after the cursor has died.MongoError.LogicErrorif this function is called and the session associated with this cursor is inactive.DecodingErrorif an error occurs decoding the server’s responses.
-
Calls the provided closure with each element in the cursor.
If the cursor is not tailable, this method will exhaust it, calling the closure with every document.
If the cursor is tailable, the method will call the closure with each new document as it arrives.
A thread from the driver’s internal thread pool will be occupied until the returned future is completed, so performance degradation is possible if the number of polling cursors is too close to the total number of threads in the thread pool. To configure the total number of threads in the pool, set the
MongoClientOptions.threadPoolSizeoption during client creation.Note: You must not call any cursor methods besides
killandisAlivewhile the future returned from this method is unresolved. Doing so will result in undefined behavior.Declaration
Swift
public func forEach(_ body: @escaping (T) throws -> Void) -> EventLoopFuture<Void>Return Value
An
EventLoopFuture<Void>which will succeed when the end of the cursor is reached, or in the case of a tailable cursor, when the cursor is killed viakill.If the future evaluates to an error, that error is likely one of the following:
MongoError.CommandErrorif an error occurs while fetching more results from the server.MongoError.LogicErrorif this function is called after the cursor has died.MongoError.LogicErrorif this function is called and the session associated with this cursor is inactive.DecodingErrorif an error occurs decoding the server’s responses.
-
Kill this cursor.
This method MAY be called even if there are unresolved futures created from other
MongoCursormethods.This method MAY be called if the cursor is already dead. It will have no effect.
Warning
On Swift versions and platforms where structured concurrency is not available, if a cursor is alive when it goes out of scope, it will leak resources. On those Swift versions/platforms, you must invoke this method to ensure resources are properly cleaned up. If structured concurrency is available, it is not necessary to call this method as resources will be cleaned up automatically during deinitialization.
Declaration
Swift
public func kill() -> EventLoopFuture<Void>Return Value
An
EventLoopFuturethat evaluates when the cursor has completed closing. This future should not fail. -
Declaration
Swift
public typealias AsyncIterator = MongoCursor -
Declaration
Swift
public typealias Element = T -
Declaration
Swift
public func makeAsyncIterator() -> MongoCursor<T> -
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()returnsnilor a non-DecodingErrorerror.This cursor will be dead after
next()returnsnilor throws a non-DecodingErrorerror, regardless of the cursor type.This cursor may still be alive after
next()ortryNext()throws aDecodingError.Declaration
Swift
public func isAlive() async throws -> Bool -
Returns the next
Tin this cursor, ornilif the cursor is exhausted or the currentTaskis cancelled.If this cursor is tailable, this method will continue polling until a non-empty batch is returned from the server, or until the
Taskit is running in is cancelled. For this reason, we recommend to run tailable cursors in their ownTasks, and to terminate the cursor if/when needed by canceling theTask.Warning
You must not call any cursor methods besides
isAlive()while awaiting the result of this method. Doing so will result in undefined behavior.If an error is thrown, it is likely one of the following:
MongoError.CommandErrorif an error occurs while fetching more results from the server.MongoError.LogicErrorif this function is called after the cursor has been exhausted.MongoError.LogicErrorif this function is called and the session associated with this cursor has been ended.DecodingErrorif an error occurs decoding the server’s response to aT.
Declaration
Swift
public func next() async throws -> T?Return Value
The next
Tin this cursor, ornilif the cursor is exhausted or the currentTaskis cancelled. -
Attempt to get the next
Tfrom the cursor, returningnilif there are no results.If this cursor is tailable, this method may be called repeatedly while
isAlive()returns true to retrieve new data.If this cursor is a tailable await cursor, it will wait for results server side for a maximum of
maxAwaitTimeMSbefore evaluating tonil. This option can be configured via options passed to the method that created this cursor (e.g. themaxAwaitTimeMSoption on theFindOptionspassed tofind).Warning
You must not call any cursor methods besides
isAlive()while awaiting the result of this method. Doing so will result in undefined behavior.If an error is thrown, it is likely one of the following: -
MongoError.CommandErrorif an error occurs while fetching more results from the server. -MongoError.LogicErrorif this function is called after the cursor has been exhausted. -MongoError.LogicErrorif this function is called and the session associated with this cursor has been ended. -DecodingErrorif an error occurs decoding the server’s response to aT.Declaration
Swift
public func tryNext() async throws -> T?Return Value
The next
Tin this cursor, ornilif there is no new data. -
Consolidate the currently available results of the cursor into an array of type
T.If this cursor is not tailable, this method will exhaust it.
If this cursor is tailable,
toArraywill only fetch the currently available results, and it may return more data if it is called again while the cursor is still alive.Warning
You must not call any cursor methods besides
isAlive()while awaiting the result of this method. Doing so will result in undefined behavior.If an error is thrown, it is likely one of the following: -
MongoError.CommandErrorif an error occurs while fetching more results from the server. -MongoError.LogicErrorif this function is called after the cursor has been exhausted. -MongoError.LogicErrorif this function is called and the session associated with this cursor has been ended. -DecodingErrorif an error occurs decoding the server’s responses toTs.Declaration
Swift
public func toArray() async throws -> [T]Return Value
An
Tcontaining the results currently available in this cursor.
View on GitHub
Install in Dash
MongoCursor Class Reference