2014-06-17 7 views
9

Ich habe eine vierstufige Aggregation Abfrage in Form einer Übereinstimmung -> Gruppe -> Projekt -> Sortieren. Die Aggregation funktioniert einwandfrei und erzeugt ein Array wie das Folgende.mongodb Aggregationsfilter für Datensätze mit einem Feld größer oder gleich einer Zahl

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

Unten ist meine Aggregation Abfrage. Gibt es eine Möglichkeit für mich, nur Ergebnisse anzuzeigen, bei denen das Feld "Anzahl" größer als 500 ist? Ich habe versucht, die Projektphase ohne Glück hinzuzufügen.

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

Die Frage ist wahrscheinlich nützlicher als die Antwort! –

Antwort

16

Sie können mehr $match Stufen in der Pipeline, sind so ein zweites $match am Ende hinzufügen:

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

Ich tat dies, aber mit einer $ -Projektanweisung, änderte es, um zu passen und es funktioniert jetzt Danke! Wird in 2 Minuten als gelöst markieren :) – isaac9A

Verwandte Themen