-
Notifications
You must be signed in to change notification settings - Fork 163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why aggregation methods return Cursor<Document>
instead of Cursor<T>
?
#1098
Comments
Aggregate<Document>
instead of Aggregate<T>
?Cursor<Document>
instead of Cursor<T>
?
Cursor<Document>
instead of Cursor<T>
?Cursor<Document>
instead of Cursor<T>
?
Hey, thanks for opening this issue! This is a great idea. For some historical context, when we originally added generics to the return types from collection methods, all of the impl<T> Collection<T> {
pub async fn aggregate<U>(...) -> Result<Cursor<U = Document>> { ... }
} So we opted to stick with
Edit: misread your code example a bit, I'm going to play around with this to figure out the exact API we want! |
Hey @isabelatkinson, this is great news! My example definitely doesn't have the right API, more than anything it was just to illustrate that there weren't glaring type system issues with something like this. That being said I hear what you're saying and it makes sense that tying the What would be nice is the serde semantics, where the aggregate method accepts it's own type parameter that implements the required traits for deserialization. Sure, users would always have to specify the type they want to deserialize to, but from my more limited perspective, that developer experience is far more tolerable than dealing with let cursor: Cursor<Foobar> = collection.aggregate(...)
// or
let cursor = collection.aggregate::<Foobar>() |
Hey, I just put up a draft PR to add a #[derive(Deserialize)]
struct PipelineOutput {
len: usize,
}
// returns Cursor<PipelineOutput>
let aggregate_cursor = collection
.aggregate(pipeline)
.with_type::<PipelineOutput>()
.await?; This change adds the functionality you're requesting without breaking any existing code that depends on a |
@isabelatkinson this is a great usability improvement. Works for me! |
Just merged #1100, going to close this out |
I'm fighting an issue right now where my
find
queries are returningCursor<T>
but my aggregate queries are returningCursor<Document>
and when I map the stream toT
my trait implementationswhere S: Stream<Item=Result<T, E>>
no longer work for the mapped cursor.That's a long way of saying the aggregation cursor user experience feels a bit worse than the other methods. This issue was also reported here: https://www.mongodb.com/community/forums/t/get-specific-data-type-from-aggregation-instead-of-document/188241 but I never saw any response.
I assume there's a reason behind aggregation cursors not returning
Cursor<T>
but I'm not sure what it is, because with the following quick and dirty patch, I was able to run the test suite and get aggregates to returnCursor<T>
. Is there some reason I'm missing why this can't be officially supported?The text was updated successfully, but these errors were encountered: