MongoDB count distinct value?

前端 未结 2 1413
夕颜
夕颜 2020-12-11 04:57

Here below show my code. I have to calculate the how many times distinct value repeated. Here i have store distinct value in \"results\".I used collection.count() to calcula

相关标签:
2条回答
  • 2020-12-11 05:44

    While .distinct() works well for just obtaining the distinct values for a field, in order to actually get the counts of occurrences, this is better suited to the aggregation framework:

    Collection.aggregate([
        { "$group": {
            "_id": "$field",
            "count": { "$sum": 1 }
        }}
    ],function(err,result) {
    
    });
    

    Also the .distinct() method does "abstract" from where the specified "distinct" field is actually within an array. In this case you need to call $unwind first to process the array elements here:

    Collection.aggregate([
        { "$unwind": "$array" },
        { "$group": {
            "_id": "$array.field",
            "count": { "$sum": 1 }
        }}
    ],function(err,result) {
    
    });
    

    So the main work is basically done in the $group by "grouping" on the field values, which means the same thing as "distinct". The $sum is a grouping operator which in this case just adds up 1 for each occurrence of that value in the field for that collection.

    0 讨论(0)
  • 2020-12-11 05:48

    To get occurrences of distinct values of a field 'field1' on a collection 'col1' and write to a separate collection 'distinctCount'. Also allow to use disk space in case the collection is huge.

    db.col1.aggregate(
              [{$group: {
                  _id: "$field1",
                  count: { $sum : 1 }
                }}, {
                $group: {
                  _id: "$_id",
                  count: { $sum : "$count" }
                }},{
                  $out: "distinctCount"
                }],
             {allowDiskUse:true}
    )
    
    0 讨论(0)
提交回复
热议问题