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

Allow custom type to be used with aggregation

    • Type: Icon: New Feature New Feature
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 2.2.0
    • Affects Version/s: None
    • Component/s: None
    • None

      Right now, the various aggregate methods always return a Cursor<Document>, since it's likely that the output format won't match the input. However, this requires users to manually deserialize the results to their custom types rather than it happening automatically. In addition to being an ergonomic issue, this also reduces performance, as the documents all have to be deserialized to Document before their final type. 

      One challenge with providing an API for this is that the turbofish operator cannot be used in conjunction with impl Trait in an argument position, so in order to make it  ergonomic to specify the type, we may need to come up with another way. One option is to add a method to Cursor which allows the type to be changed:

      let mut cursor = coll.aggregate(vec![...], None).await?.with_type::<Cat>(); 

      Or alternatively on the future:

      let mut cursor = coll.aggregate(vec![...], None).with_type::<Cat>().await?; 

      The first one will require a bit more work because in RUST-870 cursors were updated to store the deserialized T in their buffer instead of Document, but could possibly be changed to store RawDocument instead once that API is stabilized.

      Alternatively, we could introduce a new aggregate_with_type or something that does not make use of impl Trait, though this could be considered inconsistent with our other methods.

            Assignee:
            abraham.egnor@mongodb.com Abraham Egnor
            Reporter:
            patrick.freed@mongodb.com Patrick Freed
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: