简体   繁体   English

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

[英]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. 在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);

how can I sort my result based on "Source" key. 如何根据“源”键对结果进行排序。

Thnaks 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. 还有指向MongoDB文档的链接,该链接也可能对您有帮助。 There is also a decent explanation here also . 这里也有一个不错的解释

I'm just dabbling in group and sort in MongoDB myself. 我只是自己在MongoDB中进行分组和排序。

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 将按“源”字段排序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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