MongoDB Aggregation: Counting distinct fields

前端 未结 8 2115
礼貌的吻别
礼貌的吻别 2020-12-13 04:33

I am trying to write an aggregation to identify accounts that use multiple payment sources. Typical data would be.

{
 account:\"abc\",
 vendor:\"amazon\",
}         


        
相关标签:
8条回答
  • 2020-12-13 05:10

    I do not see why somebody would have to use $group twice

    db.t2.aggregate([ { $group: {"_id":"$account" , "number":{$sum:1}} } ])
    

    This will work perfectly fine.

    0 讨论(0)
  • 2020-12-13 05:12
    db.UserModule.aggregate(
    { $group : { _id : { "companyauthemail" : "$companyauthemail", "email" : "$email" }, number : { $sum : 1 } } },
    { $group : { _id : "$_id.companyauthemail", number : { $sum : 1 } } }
    );
    
    0 讨论(0)
  • I figured this out by using the $addToSet and $unwind operators.

    Mongodb Aggregation count array/set size

    db.collection.aggregate([
    {
        $group: { _id: { account: '$account' }, vendors: { $addToSet: '$vendor'} }
    },
    {
        $unwind:"$vendors"
    },
    {
        $group: { _id: "$_id", vendorCount: { $sum:1} }
    }
    ]);
    

    Hope it helps someone

    0 讨论(0)
  • 2020-12-13 05:26

    This approach doesn't make use of $unwind and other extra operations. Plus, this won't affect anything if new things are added into the aggregation. There's a flaw in the accepted answer. If you have other accumulated fields in the $group, it would cause issues in the $unwind stage of the accepted answer.

    db.collection.aggregate([{
        "$group": {
            "_id": "$account",
            "vendors": {"$addToSet": "$vendor"}
        }
    },
    {
        "$addFields": {
            "vendorCount": {
                "$size": "$vendors"
            }
        }
    }])
    
    0 讨论(0)
  • 2020-12-13 05:29

    I think its better if you execute query like following which will avoid unwind

    db.t2.insert({_id:1,account:"abc",vendor:"amazon"});
    db.t2.insert({_id:2,account:"abc",vendor:"overstock"});
    
    
    db.t2.aggregate(
    { $group : { _id : { "account" : "$account", "vendor" : "$vendor" }, number : { $sum : 1 } } },
    { $group : { _id : "$_id.account", number : { $sum : 1 } } }
    );
    

    Which will show you following result which is expected.

    { "_id" : "abc", "number" : 2 }
    
    0 讨论(0)
  • 2020-12-13 05:30

    You can use sets

    db.test.aggregate([
        {$group: { 
          _id: "$account", 
          uniqueVendors: {$addToSet: "$vendor"}
        }},
        {$project: {
          _id: 1, 
          vendorsCount: {$size: "$uniqueVendors"}
        }}
    ]);
    
    0 讨论(0)
提交回复
热议问题