如何在PHP MongoDB中按结果对组进行排序

时间:2017-01-11 20:15:41

标签: javascript php mongodb

在php中我已经创建了一个游标,它通过以下命令按照MongoDB的结果保存组。

$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);

如何根据“Source”键对结果进行排序。

Thnaks

2 个答案:

答案 0 :(得分:0)

I would refer you to this question,看看是否有帮助。还有一个指向MongoDB文档的链接也可能对您有所帮助。 There is also a decent explanation here also

我只是在团队中涉猎并在MongoDB中排序。

我发现的一个例子是;

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

and there is also a similar thread here as well.

答案 1 :(得分:0)

找到替代解决方案

$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']);

将按源字段

排序