Hi,
I'm new to MongoDB and to C#.
Please apologize if this is the wrong place to ask (rather a C# mongodb driver 2.0 question)
I currently have a main collection 'event' which I successfully aggregate into a 'myaggregate' collection, i.e
> db.event.aggregate(
... [
... { $match: {"txn_date_time" :{$gt : new Date(ISODate().getTime() - 1000 * 60 * 60 * 24)}}},
... { $group: {"_id": "$account_ref", "amount_last_day":{$sum:"$amt"} }},
... { $out: "myaggregate"}
... ])
>
> db.myaggregate.findOne()
{ "_id" : "487383", "amount_last_day" : 44.31999999999999 }
The aggregation query above was ported to a C# class where code looks like
await evenCollection.aggregate()
.Match(X => X.txn_date_time > DateTime.Now.AddHours(-24))
.Group(x => x.account_ref, g => new { Account = g.Key, amount_last_day = g.Sum(x => x.amt)})
.OutAsync("myaggregate");
I can confirm that this C# code provides the expected result, i.e correct "myaggregate" collection is created.
Now I can succesfullly query this collection (find by Id) from mongo shell :
> db.myaggregate.find({"_id" : 487383"})
{ "_id" : 487383", "amount_last_day" : 44.31999999999999 }
BUT my C# translation of this trivial query does not work as expected :
I created a ""Agg" C# class to represent a ""myaggregate" entity
i.e
[BsonIgnoreExtraElements]
public class Agg
{
[BsonId]
public string Id { get; set; } // please note this is no ObjectId
public double totalAmount { get; set; }
}
and
my c# query goes like this
[..]
string criteria ="487383";
Task<Agg> task = coll.Find(x => x.Id == criteria).FirstOrDefaultAsync();
Task.WaitAll(task);
Agg result = task.Result;
I don't understand why my result value is always 'null' where I was expecting an instance mapping the same result we got above :
{ "_id" : 487383", "amount_last_day" : 44.31999999999999 })
Could you point me to where the problem is ? In My 'Agg' entity class ? In my c# Task for querying the "myaggregate" collection ?
Thanks