Querying Aggregated collection with C# driver (v2.0.1)

55 views
Skip to first unread message

Eric Le Goff

unread,
Sep 15, 2015, 7:49:48 AM9/15/15
to mongod...@googlegroups.com
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
--
Eric Le Goff


Craig Wilson

unread,
Sep 15, 2015, 9:25:42 AM9/15/15
to mongodb-user
Hi Eric,

In your shell query, it appears that there might be a typo as the shell syntax listed is illegal. I don't intend to be pedantic, but MongoDB considers strings and integers different values. It kind of appears as though your shell query may using integers, while your Agg class is using strings for the "_id" field. Could you clarify?

Craig

Eric Le Goff

unread,
Sep 15, 2015, 9:37:02 AM9/15/15
to mongod...@googlegroups.com
Hi Craig,

My  "event" collection has a "account_ref" field which actually contains alphanumeric values. My example was unfortunate since the chosen account was "487383" but could have been "foo42" instead.

> db.myaggregate.find({"_id" : "foo42"})
{ "_id" : "foo42", "amount_last_day" : 42.5 }


This is the reason why  Agg class contains a 
public  string Id { get; set; } but I can't make any Find request on 'myagregate' so that I get the corresponding instance.

I hope this is more clear

Thanks

Eric


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/f1ca9b13-6c09-4677-b184-2fb1a0d96c86%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Craig Wilson

unread,
Sep 15, 2015, 10:11:41 AM9/15/15
to mongodb-user
Hi Eric,

Perhaps you could simply post a full program I can try? I have attempted to reproduce this and am having no issues.

Craig

Eric Le Goff

unread,
Sep 16, 2015, 4:09:02 AM9/16/15
to mongod...@googlegroups.com
Craig,

Thanks for your help. I was finally able to have a successful result. The code samples I provided were simplified and were indeed running fine (as you also experienced)

It seems the problem was related to case sensitivity : the member name in the Agg class (public double totalAmount { get; set; }) had to match exactly the aggegrated field in my query
 await evenCollection.aggregate()
                .Match(X => X.txn_date_time > DateTime.Now.AddHours(-24))
                .Group(x => x.account_ref, g => new { Account = g.Key, TotalAmount = g.Sum(x => x.amt)})
                .OutAsync("myaggregate");



Reply all
Reply to author
Forward
0 new messages