如何通过jenssegers将MongoDB聚合查询转换为Laravel MongoDB

我有一个名为更改</ strong>的MongoDB集合,其中包含以下数据</ p>
\ n

  {
“date”:ISODate(“2014-06-09T00:00:00.000Z”),
“field”:“ip”,
“from”:“157.11.209.123 “,
”到“:”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” ),
“字段”:“注册”,
“从”:“旧服务”,
“到”:“一些新服务”
}
</ code> </ pre>
\ n

然后我想制作典型的SQL查询来计算事件并通过 field </ code>对其进行分组。 所以,我在MongoDB中创建了查询</ p>

  db.changes.group({
“key”:{
“field”:true
},
“initial”:{
“count”:0,
},
“reduce”:function(obj,prev){
prev.count ++;
},
});
</ code > </ pre>

它工作正常,但如何将其转换为与Laravel 4一起使用</ strong>? 我正在使用jenssegers / laravel-mongodb与mongo服务器进行通信。 </ p>

此外,我在查询中有更多条件,我已将其删除以使我的问题看起来更清晰,所以我正在寻找将该查询完全转换为laravel的解决方案,而不是 其他可能的解决方案:)。</ p>
</ div>

展开原文

原文

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"
}

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++;
    },
});

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.

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 :).

1个回答

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

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.

So use the native MongoDB way for the best performance.

doubu4826
doubu4826 以你的方式改变你的问题确实不是一个好主意。 对于那些过来并且看到所提供的答案与您提出的答案不符的人来说,这是非常误导的。 如果你发布另一个问题,我会像其他人一样乐意回答。 这里的一般情况是一个问题一个答案。 我可以回答你的其他问题但完全应该是另一个问题。 请单独发布。
6 年多之前 回复
doushi8187
doushi8187 好的,这很棒,但有一个缺点。 我需要在一段时间内(上周和上个月)计算所有内容。 我不想运行2个查询,而我可以创建一个查询。 我已附上原始查询。 如何在示例中运行自定义缩减功能?
6 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问