MongoDb.Aggregate<T> to custom object or tuple?

Viewed 17

I am working with .NET Core 6 and MongoDB. I have an aggregate pipeline that ends up $projecting known types into two objects like below.

{
  "object1": [...],
  "object2": 1
}

My code is setup like so:

var queryResult = await _myCollection.Aggregate<ExpandoObject>(pipeline).FirstOrDefaultAsync();

The types of object1 and object2 are known and I want to deserialize into those known objects. I thought I could maybe use a Tuple to do so, but that doesn't appear to be the case as it throws an exception.

var queryResult = await _myCollection.Aggregate<(List<KnownType>, int)>(pipeline).FirstOrDefaultAsync();

I am trying to avoid creating a class, but if I do it works.

public class MyCustomClass
{
    public List<KnownType> object1 { get; set; }
    public int object2 { get; set; }
}


var queryResult = await _myCollection.Aggregate<MyCustomClass>(pipeline).FirstOrDefaultAsync();

Is there any way I can achieve this without having to create a wrapper class?

0 Answers
Related