mongo-rust-driver
mongo-rust-driver copied to clipboard
RUST-1982 Aggregations with both explicit sessions and custom types aren't supported
Versions/Environment
- What version of Rust are you using?
1.79
- What operating system are you using?
Linux
- What versions of the driver and its dependencies are you using? (Run
cargo pkgid mongodb&cargo pkgid bson)
3.0.0
- What version of MongoDB are you using? (Check with the MongoDB shell using
db.version())
6.0.15
- What is your MongoDB topology (standalone, replica set, sharded cluster, serverless)?
standalone
Describe the bug
Aggregation with both an explicit session and a custom type isn't possible. While helpers for both with_type and session exist in the 3.0 API, it's impossible to use both on the same aggregation.
Both of the following examples compile fine:
async fn aggregate_with_type<T>(coll: Collection<T>) -> Result<Cursor<T>>
where
T: DeserializeOwned + Send + Sync,
{
coll.aggregate([]).with_type::<T>().into_future().await
}
async fn aggregate_with_session(
session: &mut ClientSession,
coll: Collection<Document>,
) -> Result<SessionCursor<Document>> {
coll.aggregate([]).session(session).into_future().await
}
However, when trying to combine them, a compiler error occurs regardless of the order of with_type and session:
async fn aggregate_with_session_and_type<T>(
session: &mut ClientSession,
coll: Collection<Document>,
) -> Result<SessionCursor<T>>
where
T: DeserializeOwned + Send + Sync,
{
coll.aggregate([])
.session(session)
.with_type::<T>()
.into_future()
.await
}
error[E0599]: the method `into_future` exists for struct `Aggregate<'_, ExplicitSession<'_>, T>`, but its trait bounds were not satisfied
--> src/main.rs:14:58
|
14 | coll.aggregate([]).session(session).with_type::<T>().into_future().await
async fn aggregate_with_type_and_session<T>(
session: &mut ClientSession,
coll: Collection<Document>,
) -> Result<SessionCursor<T>>
where
T: DeserializeOwned + Send + Sync,
{
coll.aggregate([])
.with_type::<T>()
.session(session)
.into_future()
.await
}
error[E0599]: no method named `session` found for struct `Aggregate<'_, ImplicitSession, T>` in the current scope
--> src/main.rs:14:41
|
14 | coll.aggregate([]).with_type::<T>().session(session).into_future().await
In the 3.x API, this is happening because the implementation of aggregate for ExplicitSession isn't generic across the type parameter of Aggregate and instead only implements it for T = Document. However, using Document rather than T seems to date back to Collection::aggregate in the 2.x API for both explicit and implicit sessions, so I'm not sure whether the restrictions for explicit and implicit sessions are intended to be different, if the restriction for explicit sessions is unintentional, or if the relaxation of the restriction for implicit sessions was unintentional.
For context, I discovered this when attempting to use RawDocumentBuf as the output type of an aggregation with an explicit session. From what I understand, the type being returned by the aggregation shouldn't matter to the session type and vice-versa, but I figured it's worth checking to see if there's something I'm missing.
Nope, nothing subtle here, just an oversight :) I'll get a fix in shortly.
Awesome, thanks so much!
We just released version 3.0.1 that includes the fix for this.
Thanks!