mongodb聚合过滤器,用于具有大于或等于数字的字段的记录

时间:2014-06-17 16:45:11

标签: node.js mongodb aggregation-framework

我有一个匹配形式的四阶段聚合查询 - >组 - >项目 - >分类。聚合工作正常,并生成如下所示的数组。

    { count: 48, ISP: 'LEASEWEB USA', percentRisky: 100 },
    { count: 51, ISP: 'ARETI INTERNET LTD', percentRisky: 100 },
    { count: 82, ISP: 'TINET SPA', percentRisky: 100 },
    { count: 109, ISP: 'GIGLINX', percentRisky: 100 },
    { count: 142, ISP: 'EGIHOSTING', percentRisky: 100 },
    { count: 857, ISP: 'VERSAWEB, LLC', percentRisky: 100 }

以下是我的聚合查询。我有什么方法可以只显示“计数”的结果吗?场大于500?我试过加入项目阶段没有运气。

        { $match : { //match to documents which are from all clients, from last three days, and scored
            org : {"$in" : clientArray },
            frd : {$gt : new Date(JSON.stringify(util.lastXDates( 3 )))},
            sl : true 
        }},
        { $group : { //group by isp, get total count, and get count of risky
            _id : "$gog",
            count : { $sum : 1 },
            countRisky : { $sum : { $cond : { if : { $gte : [ "$scr", 65 ] } ,
                then : 1,
                else : 0
            }} }
        }},
        { $project : { //rename _id to isp, only show percent risky, and the count
            _id : 0,
            ISP : "$_id",
            percentRisky : { $multiply : [{ $divide : ["$countRisky", "$count"] }, 100] },
            count : 1
        }},
        { $sort : { //sort by percent risky
            percentRisky : 1,
            count : 1

1 个答案:

答案 0 :(得分:16)

您可以在管道中添加多个$match阶段,因此最后添加第二个$match

...
{$match: {count: {$gt: 500}}}