开发者

C# Mongo Driver - MongoCollection.Group method

开发者 https://www.devze.com 2023-02-07 17:22 出处:网络
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

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!


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.


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
);
0

精彩评论

暂无评论...
验证码 换一张
取 消