基于多个查询字段的MongoDB聚合计数 - (多字段计数)

ven*_*t.s 2 mapreduce mongodb mongodb-query aggregation-framework

我的收藏会看起来像,

{
    "_id" : ObjectId("55c8bd1d85b83e06dc54c0eb"),
    "name" : "xxx",
    "salary" : 10000,
    "type" : "type1"
}
{
    "_id" : ObjectId("55c8bd1d85b83e06dc54c0eb"),
    "name" : "aaa",
    "salary" : 10000,
    "type" : "type2"
}
{
    "_id" : ObjectId("55c8bd1d85b83e06dc54c0eb"),
    "name" : "ccc",
    "salary" : 10000,
    "type" : "type2"
}
Run Code Online (Sandbox Code Playgroud)

我的查询参数将会出现,

{salary = 10000,type = type2}

所以基于查询我需要获取上述查询参数的计数

结果应该是这样的,

{category:'type1',count:500} {category:'type2',count:200} {category:'name',count:100}

现在我通过点击三个不同的查询并构造结果(或)服务器端迭代来获得计数,我可以得到结果.

任何人都可以建议或提供我获得上述结果的好方法

Bla*_*ven 5

您的查询不是很清楚,但您在此处想要做的是计算字段中数据的出现次数,可选择按照与条件匹配的值过滤这些字段.

在这里,$cond运算符允许您将逻辑条件转换为值:

db.collection.aggregate([
    { "$group": {
        "_id": null,
        "name": { "$sum": 1 },
        "salary": { 
            "$sum": { 
                "$cond": [
                    { "$gte": [ "$salary", 1000 ] },
                    1,
                    0
                ]
            }
        },
        "type": { 
            "$sum": { 
                "$cond": [
                    { "$eq": [ "$type", "type2" ] },
                    1,
                    0
                ]
            }
        }
    }}
])
Run Code Online (Sandbox Code Playgroud)

所有值都在同一个文档中,将它们拆分在这里并没有任何意义,因为这是管道中的额外工作.

{ "_id" : null, "name" : 3, "salary" : 3, "type" : 2 }
Run Code Online (Sandbox Code Playgroud)

否则,在长形式中,由于需要为每个键创建每个文档的副本而不是非常高效,如下所示:

db.collection.aggregate([
  { "$project": {
    "name": 1,
    "salary": 1,
    "type": 1,
    "category": { "$literal": ["name","salary","type"] }
  }},
  { "$unwind": "$category" },
  { "$group": {
    "_id": "$category",
    "count": {
      "$sum": {
        "$cond": [
          { "$and": [ 
            { "$eq": [ "$category", "name"] },
            { "$ifNull": [ "$name", false ] }
          ]},
          1,
          { "$cond": [
            { "$and": [
              { "$eq": [ "$category", "salary" ] },
              { "$gte": [ "$salary", 1000 ] }
            ]},
            1,
            { "$cond": [
              { "$and": [
                { "$eq": [ "$category", "type" ] },
                { "$eq": [ "$type", "type2" ] }
              ]},
              1,
              0
            ]}
          ]}
        ]
      }
    }
  }}
])
Run Code Online (Sandbox Code Playgroud)

它的输出:

{ "_id" : "type",   "count" : 2 }
{ "_id" : "salary", "count" : 3 }
{ "_id" : "name",   "count" : 3 }
Run Code Online (Sandbox Code Playgroud)

如果您的文档没有统一的密钥名称,或者无法在管道条件中指定每个密钥,则应用mapReduce代替:

db.collection.mapReduce(
    function() {
        var doc = this;
        delete doc._id;

        Object.keys(this).forEach(function(key) {
          var value = (( key == "salary") && ( doc[key] < 1000 ))
              ? 0
              : (( key == "type" ) && ( doc[key] != "type2" ))
              ? 0
              : 1;

          emit(key,value);
        });
    },
    function(key,values) {
        return Array.sum(values);
    },
    {
        "out": { "inline": 1 }
    }
);
Run Code Online (Sandbox Code Playgroud)

它的输出:

    "results" : [
            {
                    "_id" : "name",
                    "value" : 3
            },
            {
                    "_id" : "salary",
                    "value" : 3
            },
            {
                    "_id" : "type",
                    "value" : 2
            }
    ]
Run Code Online (Sandbox Code Playgroud)

这与条件计数基本相同,只是您只指定了所需条件的"反向",并且仅指定要过滤条件的字段.当然,这种输出格式很容易作为单独的文档发出.

同样的方法适用于在需要条件的字段上测试满足条件的情况,并1在满足条件的地方返回,或者0在不计算条件的情况下返回.