然后使用c#对Mogodb聚合组进行条件求和

时间:2018-04-20 10:47:44

标签: c# mongodb mongodb-.net-driver

我有一个包含以下示例数据的成员集合

{ "_id" : ObjectId("50f6632d89862009f4c80e0f"), "_Ten" : ObjectId("50f6632d89862009f4c80e0e"), "active" : true, "role" : "Administrator", "_t" : "User" }
{ "_id" : ObjectId("50fe895678415503d022945f"), "_Ten" : ObjectId("50f6632d89862009f4c80e0e"), "active" : true, "role" : "Contributor", "_t" : "User" }
{ "_id" : ObjectId("51013a6c7841550820ad7945"), "_Ten" : ObjectId("51013a6c7841550820ad7944"), "active" : true, "role" : "Administrator", "_t" : "User" }

我需要通过“_Ten”找到不同类型用户组的总和 我在robomongo中写了以下查询并得到了结果

db.getCollection('Member').aggregate( 
        [
            {
                $match:
                {
                    $and: [
                        {
                            "active": true
                        },
                        {
                            "_t": "User"
                        }
                    ]
                }
            },
            {
                "$group":
                {
                    "_id": "$_Ten",
                    "administrators":
                    {
                        "$sum":
                        {
                            "$cond": [
                                {
                                    $eq: ["$role", "Administrator"]
                                }, 1, 0]
                        }
                    },
                    "contributers":
                    {
                        "$sum":
                        {
                            "$cond": [
                                {
                                    $eq: ["$role", "Contributor"]
                                }, 1, 0]
                        }
                    },
                    "visitors":
                    {
                        "$sum":
                        {
                            "$cond": [
                                {
                                    $eq: ["$role", "Visitor"]
                                }, 1, 0]
                        }
                    }
                }
            }
        ]
)

但我需要使用c#的结果,所以在c#中我尝试了以下

criteriaFilter = Builders<BsonDocument>.Filter.Eq("active", true);
        query = query & criteriaFilter;

        criteriaFilter = Builders<BsonDocument>.Filter.Eq("_t", "User");
        query = query & criteriaFilter;

        criteriaFilter = Builders<BsonDocument>.Filter.In<ObjectId>("_Ten", tenantIds.Select(tenantId => new ObjectId(tenantId)));
        query = query & criteriaFilter;

        var results = memberCollection.Aggregate()
                                    .Match(query)
                                    .Group(BsonDocument.Parse("{ \"$group\" : { \"_id\" : \"$_Ten\", \"administrators\" : { \"$sum\" : { \"$cond\": [{ $eq: [ \"$role\", \"Administrator\"]}, 1, 0]}}, \"contributers\" : { \"$sum\" : { \"$cond\": [{ $eq: [ \"$role\", \"Contributor\"]}, 1, 0]}},    \"visitors\" : { \"$sum\" : { \"$cond\": [{ $eq: [ \"$role\", \"Visitor\"]}, 1, 0]}}}}"))
                                    .ToList();

给出错误“命令聚合失败:字段'$ group'必须是累加器对象”

我需要帮助来为我上面提到的mongo查询创建c#查询。

1 个答案:

答案 0 :(得分:3)

通过使用以下代码在c#

中转换我的mongo查询解决了这个问题
var results = memberCollection.Aggregate()
.Match(query)
.Group(new BsonDocument()
{
{ "_id", "$_Ten" },
{ "administrators",
new BsonDocument(
"$sum",
new BsonDocument("$cond", BsonSerializer.Deserialize<BsonArray>("[{ $eq: [ 
\"$role\", \"Administrator\"]}, 1, 0]") ))},
{ "contributors",
new BsonDocument(
"$sum",
new BsonDocument("$cond", BsonSerializer.Deserialize<BsonArray>("[{ $eq: [ 
\"$role\", \"Contributor\"]}, 1, 0]") ))},
{ "visitors",
new BsonDocument(
"$sum",
new BsonDocument("$cond", BsonSerializer.Deserialize<BsonArray>("[{ $eq: [ 
\"$role\", \"Visitor\"]}, 1, 0]") ))}
}).ToList();