title: How to use Mongodb::cursor in Rust?
body: <p>It's a very simple project to learn how to use mongodb with Rust. I'm using the official mongodb driver here: <a href="https://github.com/mongodb/mongo-rust-driver" rel="nofollow noreferrer">https://github.com/mongodb/mongo-rust-driver</a>. The problem is that if I'm using <code>aggregate</code>, I cannot read the result</p>
  <pre><code>// main.rs
  use mongodb::bson::{doc, Bson};
  use mongodb::{options::AggregateOptions, options::ClientOptions, Client};
  use std::error::Error;
  use tokio;
  
  #[tokio::main]
  async fn main() -&gt; Result&lt;(), Box&lt;dyn Error&gt;&gt; {
  
     // Load the MongoDB connection string:
     let client_uri = &quot;mongodb://127.0.0.1:27017&quot;;
  
     // A Client is needed to connect to MongoDB:
     let mut options = ClientOptions::parse(&amp;client_uri).await?;
     options.app_name = Some(&quot;testing&quot;.to_string());
     let client = Client::with_options(options)?;
     
     // get the collection here
     let items = client.database(&quot;my_database&quot;).collection(&quot;inventory&quot;);
  
     // aggregate options and pipeline
     let pipeline = vec![doc! {&quot;$match&quot;: {&quot;name&quot;: &quot;FOO&quot;}}];
     let options = AggregateOptions::builder().allow_disk_use(true).build();
  
     // I'm using tokio for async-await library
     let data = items
        .aggregate(pipeline, options)
        .await
        .map_err(|e| println!(&quot;{}&quot;, e));
  
      // data is a Result&lt;mongodb::Cursor&gt; type
      match data {
         Ok(cursor) =&gt; {
          
            // I want to iterate the returned documents here
            // this doesn't compiles
            while let Some(doc) = cursor.next().await {
                println!(&quot;{}&quot;, doc?)
            }
         },
         Err(e) =&gt; println!(&quot;{:?}&quot;, e),
  }
  </code></pre>
  <p>The code above returns an error. It complains that the cursor has no <code>next()</code> function within.</p>
  <pre><code> while let Some(doc) = cursor.next().await {
     |                          ^^^^ method not found in `mongodb::Cursor`
  </code></pre>
  <p>I read the manual book for mongodb::Cursor here: <a href="https://docs.rs/mongodb/1.2.1/mongodb/struct.Cursor.html" rel="nofollow noreferrer">https://docs.rs/mongodb/1.2.1/mongodb/struct.Cursor.html</a></p>
  <p>and aggregate function here <a href="https://docs.rs/mongodb/1.2.1/mongodb/struct.Collection.html#method.aggregate" rel="nofollow noreferrer">https://docs.rs/mongodb/1.2.1/mongodb/struct.Collection.html#method.aggregate</a></p>
  <p>As you can see the aggregate method should return <code>Result&lt;Cursor&gt;</code>. As the manual stated:</p>
  <blockquote>
  <p>A cursor can be used like any other Stream. The simplest way is just
  to iterate over the documents it yields:</p>
  </blockquote>
  <pre><code>while let Some(doc) = cursor.next().await {
    println!(&quot;{}&quot;, doc?)
  }
  </code></pre>
  <p>So why is it doesn't work ?</p>
  <p>My dependencies in <code>Cargo.toml</code>:</p>
  <pre><code>[dependencies]
  tokio = { version = &quot;0.2&quot;, features = [&quot;macros&quot;, &quot;rt-threaded&quot;]  }
  serde = { version = &quot;1.0&quot;, features = [&quot;derive&quot;] }
  mongodb = &quot;1.2.0&quot;
  </code></pre>
  <p>If I print the cursor <code>println!(&quot;{:?}&quot;, cursor);</code>. It contains data in it. How to get the data out from this cursor ?</p>