Mongodb查询:聚合的匹配阶段

时间:2018-12-21 10:15:26

标签: mongodb mongodb-query

我正在解决与聚合相关的问题。

我正在启动此聚合:

db.Metriques.aggregate([
   {
      "$match":{
         "application":"RDOCUI"
      }
   },
   {
      "$project":{
         "uploadedRefs":1,
         "uploadedKb":1,
         "downloadedRefs":1,
         "downloadedKb":1,
         "year":{
            "$year":"$timestamp"
         },
         "month":{
            "$month":"$timestamp"
         },
         "day":{
            "$dayOfMonth":"$timestamp"
         }
      }
   },
   {
      "$group":{
         "_id":{
            "year":"$year",
            "month":"$month",
            "day":"$day"
         },
         "up-refs":{
            "$sum":"$uploadedRefs"
         },
         "up-kbs":{
            "$sum":"$uploadedKb"
         },
         "down-refs":{
            "$sum":"$downloadedRefs"
         },
         "down-kbs":{
            "$sum":"$downloadedKb"
         }
      }
   }
]);

结果是:

{
   "_id":{
      "year":2018,
      "month":12,
      "day":20
   },
   "up-refs":NumberLong(0),
   "up-kbs":NumberLong(0),
   "down-refs":0,
   "down-kbs":NumberLong(526)
}{
   "_id":{
      "year":2018,
      "month":12,
      "day":18
   },
   "up-refs":NumberLong(6),
   "up-kbs":NumberLong(3156),
   "down-refs":0,
   "down-kbs":NumberLong(7890)
}{
   "_id":{
      "year":2018,
      "month":12,
      "day":12
   },
   "up-refs":NumberLong(0),
   "up-kbs":NumberLong(0),
   "down-refs":0,
   "down-kbs":NumberLong(1299972)
}{
   "_id":{
      "year":2018,
      "month":11,
      "day":14
   },
   "up-refs":NumberLong(1),
   "up-kbs":NumberLong(94478),
   "down-refs":0,
   "down-kbs":NumberLong(0)
}{
   "_id":{
      "year":2018,
      "month":12,
      "day":10
   },
   "up-refs":NumberLong(0),
   "up-kbs":NumberLong(0),
   "down-refs":0,
   "down-kbs":NumberLong(0)
}{
   "_id":{
      "year":2018,
      "month":12,
      "day":11
   },
   "up-refs":NumberLong(5),
   "up-kbs":NumberLong(101468883),
   "down-refs":0,
   "down-kbs":NumberLong(1722138580)
}{
   "_id":{
      "year":2018,
      "month":12,
      "day":4
   },
   "up-refs":NumberLong(1),
   "up-kbs":NumberLong(94478),
   "down-refs":0,
   "down-kbs":NumberLong(47805868)
}

此后,我在聚合中添加了此匹配阶段:

{
   "$match":{
      "$or":[
         {
            "up-refs":{
               "$gte":{
                  "$numberLong":"1"
               }
            }
         },
         {
            "up-kbs":{
               "$gte":{
                  "$numberLong":"10"
               }
            }
         }
      ]
   }
}

尽管如此,结果还是空的。

完整的汇总是:

db.Metriques.aggregate([
   {
      "$match":{
         "application":"RDOCUI"
      }
   },
   {
      "$project":{
         "uploadedRefs":1,
         "uploadedKb":1,
         "downloadedRefs":1,
         "downloadedKb":1,
         "year":{
            "$year":"$timestamp"
         },
         "month":{
            "$month":"$timestamp"
         },
         "day":{
            "$dayOfMonth":"$timestamp"
         }
      }
   },
   {
      "$group":{
         "_id":{
            "year":"$year",
            "month":"$month",
            "day":"$day"
         },
         "up-refs":{
            "$sum":"$uploadedRefs"
         },
         "up-kbs":{
            "$sum":"$uploadedKb"
         },
         "down-refs":{
            "$sum":"$downloadedRefs"
         },
         "down-kbs":{
            "$sum":"$downloadedKb"
         }
      }
   },
   {
      "$match":{
         "$or":[
            {
               "up-refs":{
                  "$gte":{
                     "$numberLong":"1"
                  }
               }
            },
            {
               "up-kbs":{
                  "$gte":{
                     "$numberLong":"10"
                  }
               }
            }
         ]
      }
   }
]);

0 个答案:

没有答案