MongoDB关于内部子集合的聚合组并获得计数

时间:2018-07-05 13:12:51

标签: mongodb mongodb-query aggregation-framework mongodb-.net-driver

我有一个用户集合,其中进一步有一个“用户订阅”集合,其中还具有“订阅>发布”。

我在Mongo中关注了以下用户

/* 1 */
{
    "_id" : 1,
    "UserSubscriptions" : [ 
    {
        "_id" : 1,
        "Subscription" : {
            "_id" : 1,
            "Publication" : {
                "_id" : 1,
                "Code" : "1MM",
            },
    },
    {
        "_id" : 2,
        "Subscription" : {
            "_id" : 2,
            "Publication" : {
                "_id" : 2,
                "Code" : "2MM",
            },      
    },
    {
        "_id" : 7,
        "Subscription" : {
            "_id" : 7,
            "Publication" : {
                "_id" : 1,
                "Code" : "1MM",
            },      
    }
]
}

/* 2 */
{
    "_id" : 2,
    "UserSubscriptions" : [ 
    {
        "_id" : 3,
        "Subscription" : {
            "_id" : 3,
            "Publication" : {
                "_id" : 1,
                "Code" : "1MM",
            }      
        }
    ]
}

/* 3 */
{
    "_id" : 3,
    "UserSubscriptions" : [ 
    {
        "_id" : 4,
        "Subscription" : {
            "_id" : 4,
            "Publication" : {
                "_id" : 1,
                "Code" : "1MM",
            }      
        }
    ]
}

/* 4 */
{
    "_id" : 4,
    "UserSubscriptions" : [ 
    {
        "_id" : 5,
        "Subscription" : {
            "_id" : 5,
            "Publication" : {
                "_id" : 2,
                "Code" : "2MM",
            }      
       }
   ]
}

我正在尝试获取所有的“发布代码”和计数(用户已订阅订阅计数)。所以从上面的收藏中我想要这样的结果

 PublicationCode      Count (Number of users)
 1MM                  3
 2MM                  2 

我尝试跟踪MongoDB查询,但未获得实际结果

db.runCommand(
{ aggregate : "User", pipeline : [
    {$match: { "UserSubscriptions.0": {$exists: true}} },
    {$group: {_id:"$UserSubscriptions.Subscription.Publication.Code", count:{$sum:1}}},
    {$project: {_id:1,count:1 } }
    ]}
);

请建议一些在子集合上使用分组的方法

(Robo 3T 1.2.1)(MongoDB.Driver 2.4.4)

对应的C#Mongo驱动程序查询是

var unWind = new BsonDocument
        {
            { "$unwind", "$UserSubscriptions" }
        };
var group = new BsonDocument
            {{"$group",
                new BsonDocument
                {{ "_id", new BsonDocument {{"id", 
"$UserSubscriptions.Subscription.Publication.Code"}, }},
                {"Count",  new BsonDocument {{"$sum", 1}} }}
            }};

var pipeline = new[] { unWind, group };
var ff = _readOnlyAccess.GetDatabase(this._Database).GetCollection<T> 
(this._Collection).Aggregate<T>(pipeline);

导致错误

FormatException:无法从BsonType'Document'反序列化'Int32'。

1 个答案:

答案 0 :(得分:0)

只需使用$unwind。另外,也不需要使用$match-$unwind默认会跳过空数组。

db.getCollection('User').aggregate([
  {$unwind: '$UserSubscriptions'},
  {$group: {
    _id: '$UserSubscriptions.Subscription.Publication.Code',
    count: {$sum: 1}
  }}
])

编辑:上面将多次在客户中计算多个代码,因此这是更新版本:

db.getCollection('User').aggregate([
  {$unwind: '$UserSubscriptions'},
  {$group: {
    _id: '$_id',
    codes: {$addToSet: '$UserSubscriptions.Subscription.Publication.Code'}
  }},
  {$unwind: '$codes'},
  {$group: {
    _id: '$codes',
    count: {$sum: 1}
  }}
])