Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB$组聚合_Mongodb_Mongodb Query_Aggregation Framework_Aggregation - Fatal编程技术网

MongoDB$组聚合

MongoDB$组聚合,mongodb,mongodb-query,aggregation-framework,aggregation,Mongodb,Mongodb Query,Aggregation Framework,Aggregation,我有这样的收藏 OrgName EmpId Domain Date Google 12345 ABC 2017/01/01 Google 12345 XYZ 2017/02/01 Google 67890 ABC 2017/03/01 Google 45678 ABC 2017/03/02 Yahoo 69875 HGF 2017/03/02 Google 45678 XYZ 2017/03/03 Google 4567

我有这样的收藏

OrgName EmpId   Domain    Date
Google  12345   ABC   2017/01/01
Google  12345   XYZ   2017/02/01 
Google  67890   ABC   2017/03/01
Google  45678   ABC   2017/03/02
Yahoo   69875   HGF   2017/03/02
Google  45678   XYZ   2017/03/03
Google  45678   XYZ   2017/03/03
Google  12345   XYZ   2017/03/03
Google  12345   ABC   2017/03/04
Google  12345   ABC   2017/04/05
我需要获取哪个员工拥有最大的域计数,并且必须在ABC和XYZ域GROUPBY OrgName wise中

我正在使用以下查询:

db.Collection1.aggregate([{ "$match" : { "$or" : [ { "Domain": "ABC"},{ "Domain": "XYZ"}]}},
{
    $group :{ "_id": {"OrgName" : "$OrgName", "EmpId" : "$EmpId",
        "Domain" : "$Domain"},
            count:{ $sum : 1 },
            "participantData" : { "$push" : { "EmpId" : "$EmpId" , "Domain" : "$Domain"}}}},
    {$sort:{"count":-1}},
     {$limit: 10}
],{ allowDiskUse: true })

在上面的示例中,我期望的结果是:ABC和XYZ域计数中的employee_id=12345都是5,即12345.ABC=3和12345.XYZ=2。

您可以尝试下面的查询

下面的查询$group by OrgName,EmpId后跟$match,用于筛选参与者数组同时包含“ABC”和“XYZ”值的文档

$按计数对过滤后的数据进行排序,并输出前10个值

db.collection.aggregate([
  {"$match":{"$or":[{"Domain":"ABC"},{"Domain":"XYZ"}]}},
  {"$group":{
    "_id":{"OrgName":"$OrgName","EmpId":"$EmpId"},
    "count":{"$sum":1},
    "participantData":{"$push":{"EmpId":"$EmpId","Domain":"$Domain"}}
  }},
  {"$match":{"participantData.Domain":{"$all":["ABC","XYZ"]}}},
  {"$sort":{"count":-1}},
  {"$limit":10}
])

这里有问题吗?@KirkLarkin是的!我有一个最新的问题。问题:我需要通过OrgName、EmpId获取CountDomain,其中Domain=ABC和Domain=XYZ组