Given a class with IDictionary member
public class A
{
...
[BsonElement("cli")]
public Guid ClientId { get; set; }
[BsonDictionaryOptions(DictionaryRepresentation.Document)]
[BsonElement("props")]
public IDictionary<string, object> Properties { get; set; }
...
}
I'm trying to build an expression tree for GroupBy for MongoDB C# driver specifically. For example:
var test = baseQuery.GroupBy(x => new Tuple<Guid, object>(x.ClientId, x.Properties["Location"]));
My problem is on emitting correct Expression for x.Properties["Location"]
that is acceptable by Mongo.
If I attempt to use PropertyIndexer as explained in another stackoverflow question which basically emits {x.Properties.Item["Location"]
, Mongo will complain:
System.NotSupportedException: $project or $group does not support {document}{props}.Item["Location"].
I suspect the offending issue is the extra "Item" on the expression, as I can successfully execute the query above without reflection.
How can I access the Dictionary as this[TKey]
as mentioned in the learn.microsoft.com via reflection?
Many Thanks!
Update
Sample mongo document is as follows
{
"_id" : ObjectId("5bad9a7b73d552637cc97b58"),
"cli" : NUUID("02c39a78-316a-4756-b88c-b8313f6ef252"),
...
"props" : {
"Phone" : NumberLong(442076),
"Location" : "North Ryde",
"Start" : ISODate("2018-09-01T03:19:16.000Z"),
}
}