-
Type: Task
-
Resolution: Won't Fix
-
Priority: 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
.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?