mongodb聚合过滤器,用于字段大于或等于数字的记录 [英] mongodb aggregation filter for records with a field greater than or equal to a number

查看:369
本文介绍了mongodb聚合过滤器,用于字段大于或等于数字的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a four stage aggregation query in the form of a match -> group -> project -> sort. The aggregation works fine and produces an array such as the following.

    { 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的结果?我尝试添加到项目阶段没有运气.

Below is my aggregation query. Is there any way for me to only show results where the 'count' field is greater than 500? I have tried adding to the project stage with no luck.

        { $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

推荐答案

您可以在管道中包含多个$match阶段,因此请在末尾添加第二个$match:

You can include multiple $match stages in your pipeline, so add a second $match at the end:

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

这篇关于mongodb聚合过滤器,用于字段大于或等于数字的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆