cursor = db.flow.aggregate([
    {
        $match : {
            flowStat : { $bitsAllClear : NumberInt(0x1) },  // 'A' flows only
            pwxType : { $ne : 0 },
            timeFirst : { $gte : time_from },
            timeLast  : { $lte : time_to },
        }
    },
    {
        $project : {
            pwxType : "$pwxType",
            pwxUser : "$pwxUser",
            pwxPass : "$pwxPass",
            sip : {
                $cond : {
                    if : {
                        $eq : [
                            { $mod : [ "$flowStat", 2 ] },
                            0
                        ]
                    },
                    then : "$srcIP",
                    else : "$dstIP"
                }
            },
            dip : {
                $cond : {
                    if : {
                        $eq : [
                            { $mod : [ "$flowStat", 2 ] },
                            0
                        ]
                    },
                    then : "$dstIP",
                    else : "$srcIP"
                }
            },
        }
    },
    {
        $match : {
            $or : [
                { pwxUser : { $ne : "" } },
                { pwxPass : { $ne : "" } },
            ]
        }
    },
    {
        $project : {
            user : "$pwxUser",
            pass : "$pwxPass",
            sip : "$sip",
            dip : "$dip",
            proto : {
                $switch : {
                    branches : [
                        { case : { $eq : [ "$pwxType",  1 ] }, then : "FTP" },
                        { case : { $eq : [ "$pwxType",  2 ] }, then : "POP3" },
                        { case : { $eq : [ "$pwxType",  3 ] }, then : "IMAP" },
                        { case : { $eq : [ "$pwxType",  4 ] }, then : "SMTP" },
                        { case : { $eq : [ "$pwxType",  5 ] }, then : "HTTP Basic" },
                        { case : { $eq : [ "$pwxType",  6 ] }, then : "HTTP Proxy" },
                        { case : { $eq : [ "$pwxType",  7 ] }, then : "HTTP GET" },
                        { case : { $eq : [ "$pwxType",  8 ] }, then : "HTTP POST" },
                        { case : { $eq : [ "$pwxType",  9 ] }, then : "IRC" },
                        { case : { $eq : [ "$pwxType", 10 ] }, then : "Telnet" },
                        { case : { $eq : [ "$pwxType", 11 ] }, then : "LDAP" },
                        { case : { $eq : [ "$pwxType", 12 ] }, then : "PAP" },
                    ],
                    default : "$pwxType"
                }
            }
        }
    },
    {
        $group : {
            _id : {
                sip : "$sip",
                dip : "$dip",
                proto : "$proto",
                user : "$user",
                pass : "$pass",
            },
            count : { $sum : 1 }
        }
    },
    {
        $sort : { count : -1 }
    },
    {
        $limit : n
    }
])

cursor.forEach(
    function(e) {
        print(
              e._id.sip   + "\t"
            + e._id.dip   + "\t"
            + e._id.proto + "\t"
            + e._id.user  + "\t"
            + e._id.pass  + "\t"
            + e.count
        )
    }
)
