简体   繁体   中英

how can sort group by result in PHP MongoDB

in php i have created a cursor which hold group by result from MongoDB with following command.

$m = new MongoClient();
$db = $m->Forensic;
$c= $db->mobile_data;

// JavaScript function to perform
$reduce = "function (obj, prev) { prev.count++; }";

//group by source (number of messages);
$d = $c->group( ["Source" => 1], ["count" => 0], $reduce);

how can I sort my result based on "Source" key.

Thnaks

I would refer you to this question and see if that helps. There is also a link to the MongoDB documentation that might help you as well. There is also a decent explanation here also .

I'm just dabbling in group and sort in MongoDB myself.

One example I found was;

db.orders.aggregate([
   {
     $group: {
        _id: "$cust_id",
        count: { $sum: 1 }
     }
   },
   { $match: { count: { $gt: 1 } } }
])

and there is also a similar thread here as well.

Found an alternative solution

$m = new MongoClient();
$db = $m->Forensic;
$c= $db->mobile_data;

// JavaScript function to perform
$reduce = "function (obj, prev) { prev.count++; }";

//group by source (number of messages);
$d = $c->group( ["Source" => 1], ["count" => 0], $reduce);

//sort record
sort($d['retval']);

which will sort by Source field

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM