How to convert MongoDB aggregation query to Laravel MongoDB by jenssegers(jenssegers 如何将 MongoDB 聚合查询转换为 Laravel MongoDB)
问题描述
我有一个名为 changes 的 MongoDB 集合,其中包含以下数据
I have MongoDB collection called changes which contains following data
{
"date" : ISODate("2014-06-09T00:00:00.000Z"),
"field" : "ip",
"from" : "157.11.209.123",
"to" : "107.21.109.254"
}
{
"date" : ISODate("2014-05-15T00:00:00.000Z"),
"field" : "ip",
"from" : "107.21.109.254",
"to" : "157.11.209.123"
}
{
"date" : ISODate("2014-06-09T00:00:00.000Z"),
"field" : "registration",
"from" : "Old service",
"to" : "Some new service"
}
然后我想进行典型的 SQL 查询来计算出现次数并按 field
对其进行分组.所以,我在 MongoDB 中创建了查询
Then I want to make typical SQL query that counts occurrences and group it by field
. So, I've created query in MongoDB
db.changes.group({
"key": {
"field": true
},
"initial": {
"count": 0,
},
"reduce": function(obj, prev) {
prev.count++;
},
});
它工作正常,但 我怎样才能将它转换为适用于 Laravel 4?我正在使用 jenssegers/laravel-mongodb 与 mongo 服务器进行通信.
It works fine but how can I convert it to works with Laravel 4? I am using jenssegers/laravel-mongodb to communicate with mongo server.
此外,我在查询中有更多条件,我已将其删除以使我的问题看起来更清晰,因此我正在寻找将查询完全转换为 laravel 的解决方案,而不是其他可能的解决方案:).
In addition, I have more conditions in query, which I've removed to make my question looks more clearly, so I'm looking for solution to convert exactly that query into laravel, not other possible solutions :).
推荐答案
您最好使用聚合框架方法,并深入研究底层驱动程序提供的原始 MongoDB 集合对象.尝试翻译语法是一个更好的选择:
You are both better off using the aggregation framework methods and also diving into the raw MongoDB collection object provided from the underlying driver to do so. It's a much better option that trying to translate the syntax:
// Returns the original Mongo Result
$result = DB::collection('changes')->raw(function($collection)
{
return $collection->aggregate(array(
array(
'$group' => array(
'_id' => '$field',
'count' => array(
'$sum' => 1
)
)
)
));
});
结果与 .group()
之类的方法的结果略有不同,但它使用 MongoDB 服务器上的本机代码,并且不依赖于像 .group 这样的 JavaScript 解释()
方法确实如此,它实际上是 mapReduce 的包装器.
The result is a little different from the result of a method like .group()
but this uses native code on the MongoDB server and does not rely on JavaScript interpretation like the .group()
method actually does, being really a wrapper around mapReduce.
最终结果比您脱离原生框架界面要快得多,而且通常也更有效率.
The end result is much faster, and also generally more efficient than you will get out of the native framework interface.
所以使用原生 MongoDB 方式以获得最佳性能.
So use the native MongoDB way for the best performance.
这篇关于jenssegers 如何将 MongoDB 聚合查询转换为 Laravel MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:jenssegers 如何将 MongoDB 聚合查询转换为 Laravel MongoDB
基础教程推荐
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01