Uploaded image for project: 'Rust Driver'
  1. Rust Driver
  2. RUST-1468

mongo-rust-driver - PR #739: Collect only deserializable documents

    • Type: Icon: Task Task
    • Resolution: Won't Fix
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:

      sgasse has created PR #739: Collect only deserializable documents in mongo-rust-driver

      Issue Text:
      The [documentation](https://docs.rs/mongodb/latest/mongodb/struct.Cursor.html) mentiones several ways to collect documents from a typed collection:
      ```rust
      // regular Stream uses collect() and collects into a Vec<Result<T>>
      let v: Vec<Result<_>> = cursor.collect().await;
      ```

      ```rust
      // TryStream uses try_collect() and collects into a Result<Vec<T>>
      let v: Vec<_> = cursor.try_collect().await?;
      ```

      In the project I work on, we have heterogeneous collections and often only want the documents that can be deserialized successfully. What I currently do is:

      ```rust
      let valid_documents: Vec<LongDocumentTypeName> = cursor
      .filter_map(|e| async move

      { e.ok() })
      .collect()
      .await;
      ```

      An alternative with collecting the results first before mapping them looks like this:

      ```rust
      let valid_documents: Vec<LongDocumentTypeName> = cursor
      .collect::<Vec<Result<LongDocumentTypeName, _>>>()
      .await
      .filter_map(|e| { e.ok() }

      )
      .collect();
      ```

      While I wonder what is the idiomatic way, I want to be able to retrieve the valid, deserializable documents with a one-liner. For this, I added a helper function which I tried to integrate into the MongoDB Rust driver in this PR.

      Do you think it is helpful? Am I missing some existing functionality that would allow me to get just the valid documents?

            Assignee:
            abraham.egnor@mongodb.com Abraham Egnor
            Reporter:
            dbeng-pm-bot PM Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: