Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb aggregation filter for records with a field greater than or equal to a number

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 }

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
like image 213
isaac9A Avatar asked Jun 17 '14 16:06

isaac9A


People also ask

How can you group by a particular value in MongoDB?

We can group by single as well as multiple field from the collection, we can use $group operator in MongoDB to group fields from the collection and returns the new document as result. We are using $avg, $sum, $max, $min, $push, $last, $first and $addToSet operator with group by in MongoDB.

Can we use count with aggregate function in MongoDB?

The MongoDB $count operator allows us to pass a document to the next phase of the aggregation pipeline that contains a count of the documents. There a couple of important things to note about this syntax: First, we invoke the $count operator and then specify the string.

Which aggregation method is preferred for use by MongoDB?

The pipeline provides efficient data aggregation using native operations within MongoDB, and is the preferred method for data aggregation in MongoDB. The aggregation pipeline can operate on a sharded collection. The aggregation pipeline can use indexes to improve its performance during some of its stages.


Video Answer


1 Answers

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

...
{$match: {count: {$gt: 500}}}
like image 183
JohnnyHK Avatar answered Sep 20 '22 01:09

JohnnyHK