You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use common::{GenericCursor,GetMoreProvider,GetMoreProviderResult};
23
23
24
-
/// A `Cursor` streams the result of a query. When a query is made, a `Cursor` will be returned with
25
-
/// the first batch of results from the server; the documents will be returned as the `Cursor` is
26
-
/// iterated. When the batch is exhausted and if there are more results, the `Cursor` will fetch the
27
-
/// next batch of documents, and so forth until the results are exhausted. Note that because of this
28
-
/// batching, additional network I/O may occur on any given call to `Cursor::next`. Because of this,
29
-
/// a `Cursor` iterates over `Result<Document>` items rather than simply `Document` items.
24
+
/// A [`Cursor`] streams the result of a query. When a query is made, the returned [`Cursor`] will
25
+
/// contain the first batch of results from the server; the individual results will then be returned
26
+
/// as the [`Cursor`] is iterated. When the batch is exhausted and if there are more results, the
27
+
/// [`Cursor`] will fetch the next batch of documents, and so forth until the results are exhausted.
28
+
/// Note that because of this batching, additional network I/O may occur on any given call to
29
+
/// `next`. Because of this, a [`Cursor`] iterates over `Result<T>` items rather than
30
+
/// simply `T` items.
30
31
///
31
32
/// The batch size of the `Cursor` can be configured using the options to the method that returns
32
33
/// it. For example, setting the `batch_size` field of
@@ -38,45 +39,42 @@ use common::{GenericCursor, GetMoreProvider, GetMoreProviderResult};
38
39
/// results from the server; both of these factors should be taken into account when choosing the
39
40
/// optimal batch size.
40
41
///
41
-
/// A cursor can be used like any other [`Stream`](https://docs.rs/futures/0.3.4/futures/stream/trait.Stream.html). The simplest way is just to iterate over the
42
-
/// documents it yields:
42
+
/// [`Cursor`] implements [`Stream`](https://docs.rs/futures/latest/futures/stream/trait.Stream.html), which means
43
+
/// it can be iterated over much in the same way that an `Iterator` can be in synchronous Rust. In
44
+
/// order to do so, the [`StreamExt`](https://docs.rs/futures/latest/futures/stream/trait.StreamExt.html) trait must
45
+
/// be imported. Because a [`Cursor`] iterates over a `Result<T>`, it also has access to the
46
+
/// potentially more ergonomic functionality provided by
47
+
/// [`TryStreamExt`](https://docs.rs/futures/latest/futures/stream/trait.TryStreamExt.html), which can be
48
+
/// imported instead of or in addition to
49
+
/// [`StreamExt`](https://docs.rs/futures/latest/futures/stream/trait.StreamExt.html). The methods from
50
+
/// [`TryStreamExt`](https://docs.rs/futures/latest/futures/stream/trait.TryStreamExt.html) are especially useful when
51
+
/// used in conjunction with the `?` operator.
43
52
///
44
53
/// ```rust
45
-
/// # use futures::stream::StreamExt;
46
54
/// # use mongodb::{bson::Document, Client, error::Result};
47
55
/// #
48
56
/// # async fn do_stuff() -> Result<()> {
49
57
/// # let client = Client::with_uri_str("mongodb://example.com").await?;
50
58
/// # let coll = client.database("foo").collection::<Document>("bar");
51
-
/// # let mut cursor = coll.find(None, None).await?;
52
59
/// #
60
+
/// use futures::stream::{StreamExt, TryStreamExt};
61
+
///
62
+
/// let mut cursor = coll.find(None, None).await?;
63
+
/// // regular Stream uses next() and iterates over Option<Result<T>>
53
64
/// while let Some(doc) = cursor.next().await {
54
65
/// println!("{}", doc?)
55
66
/// }
56
-
/// #
57
-
/// # Ok(())
58
-
/// # }
59
-
/// ```
60
-
///
61
-
/// Additionally, all the other methods that an [`Stream`](https://docs.rs/futures/0.3.4/futures/stream/trait.Stream.html) has are available on `Cursor` as well.
62
-
/// This includes all of the functionality provided by [`StreamExt`](https://docs.rs/futures/0.3.4/futures/stream/trait.StreamExt.html), which provides similar functionality to the standard library `Iterator` trait.
63
-
/// For instance, if the number of results from a query is known to be small, it might make sense
64
-
/// to collect them into a vector:
67
+
/// // regular Stream uses collect() and collects into a Vec<Result<T>>
68
+
/// let v: Vec<Result<_>> = cursor.collect().await;
65
69
///
66
-
/// ```rust
67
-
/// # use futures::stream::StreamExt;
68
-
/// # use mongodb::{
69
-
/// # bson::{doc, Document},
70
-
/// # error::Result,
71
-
/// # Client,
72
-
/// # };
73
-
/// #
74
-
/// # async fn do_stuff() -> Result<()> {
75
-
/// # let client = Client::with_uri_str("mongodb://example.com").await?;
76
-
/// # let coll = client.database("foo").collection("bar");
0 commit comments