6

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

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Mateusz Nowak
  • 4,021
  • 2
  • 25
  • 37

1 Answers1

11

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.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • Ok, that works awesome, but there is possible one drawback. I need to calculate everything in some period of time (within last week and last month). I don't want to run 2 queries, whilst I can make one. I've attached my original query. How can I run custom reduce function in your example? – Mateusz Nowak Jun 10 '14 at 14:38
  • 2
    @estshy It really is not a good idea to change your question the way you have. It is very misleading to people who come along and see that answers provided do not match what you have asked. If you post another question I will be as will others happy to answer it. The general case here is one question one answer. I can answer your additional question but It should be another question entirely. Please post it separately. – Neil Lunn Jun 10 '14 at 14:53