[][src]Struct mongodb::Cursor

pub struct Cursor<T = Document> where
    T: DeserializeOwned + Unpin
{ /* fields omitted */ }

A Cursor streams the result of a query. When a query is made, a Cursor will be returned with the first batch of results from the server; the documents will be returned as the Cursor is iterated. When the batch is exhausted and if there are more results, the Cursor will fetch the next batch of documents, and so forth until the results are exhausted. Note that because of this batching, additional network I/O may occur on any given call to Cursor::next. Because of this, a Cursor iterates over Result<Document> items rather than simply Document items.

The batch size of the Cursor can be configured using the options to the method that returns it. For example, setting the batch_size field of FindOptions will set the batch size of the Cursor returned by Collection::find.

Note that the batch size determines both the number of documents stored in memory by the Cursor at a given time as well as the total number of network round-trips needed to fetch all results from the server; both of these factors should be taken into account when choosing the optimal batch size.

A cursor can be used like any other Stream. The simplest way is just to iterate over the documents it yields:

while let Some(doc) = cursor.next().await {
  println!("{}", doc?)
}

Additionally, all the other methods that an Stream has are available on Cursor as well. This includes all of the functionality provided by StreamExt, which provides similar functionality to the standard library Iterator trait. For instance, if the number of results from a query is known to be small, it might make sense to collect them into a vector:

let results: Vec<Result<Document>> = cursor.collect().await;

Trait Implementations

impl<T: Debug> Debug for Cursor<T> where
    T: DeserializeOwned + Unpin
[src]

impl<T> Drop for Cursor<T> where
    T: DeserializeOwned + Unpin
[src]

impl<T> Stream for Cursor<T> where
    T: DeserializeOwned + Unpin
[src]

type Item = Result<T>

Values yielded by the stream.

Auto Trait Implementations

impl<T = Document> !RefUnwindSafe for Cursor<T>[src]

impl<T> Send for Cursor<T> where
    T: Send
[src]

impl<T = Document> !Sync for Cursor<T>[src]

impl<T> Unpin for Cursor<T>[src]

impl<T = Document> !UnwindSafe for Cursor<T>[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> StreamExt for T where
    T: Stream + ?Sized

impl<St> StreamExt for St where
    St: Stream + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<S, T, E> TryStream for S where
    S: Stream<Item = Result<T, E>> + ?Sized

type Ok = T

The type of successful values yielded by this future

type Error = E

The type of failures yielded by this future

impl<S> TryStreamExt for S where
    S: TryStream + ?Sized

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,