3

I've tried digging around on the internet everywhere, but can't find for the life of me how to use this method. I have tried looking at the mongo console to correlate the two, but my it has defeated my brain. Can anybody help me out at all with this or point me in the direction of help?

I am using the C# driver from mongodb.org.

I have the following document:

ObjectId Id ObjectId ForeignId

I would like to count the number of documents and group the count by the ForeignId field. Thanks!

4
  • Have you looked at the (non-C#) documentation on group? mongodb.org/display/DOCS/Aggregation#Aggregation-Group Commented Jan 30, 2011 at 1:34
  • Which C# driver are you using? There are a couple of options. Commented Jan 30, 2011 at 1:36
  • Yeah, I tried that. I get how to do the group in the console, but I can't translate it to the driver :( Commented Jan 30, 2011 at 1:36
  • Added the driver I'm using above Commented Jan 30, 2011 at 1:39

2 Answers 2

4

That looks slightly off (the order of the parameters). It should be:

var document = new BsonDocument("count", 0); var result = myCollection.Group<BsonDocument>( Query.Null, "ForeignId", document, new BsonJavaScript("function(doc, out){ out.count++; }"), null ); 

There's a similar example in the TestGroup unit test in MongoCollectionTests.cs.

Sign up to request clarification or add additional context in comments.

2 Comments

better late then never, it seems we have different versions of the driver :)
This only works now if you remove the generic <BsonDocument> from the Group method.
0

I THINK I answered my own question through some hacking:

var document = new BsonDocument("count", 0); myCollection.Group<BsonDocument>( "ForeignId", null, document, new BsonJavaScript("function(doc, out){ out.count++; }"), null ); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.