2

Let me get to the point, I am currently using the Doctrine MongoDB ODM in conjunction with Symfony2 to persist data into MongoDB.

Currently I am grouping my results by type, but I would like to group them by MAX(group_id) as well.

Sure I can just alter the reduce function, but I am trying to steer clear of a large return array and more processing once the query is done, so I was wondering if there is a more elegant solution than that to this particular problem.

The Monitoring document,

/**
 * @ODM\Document(collection="monitoring")
 */

class Monitoring
{


    /** @ODM\Id */
    public $id;

    /** @ODM\String */
    public $type;

    /** @ODM\String */
    public $message;

    /** @ODM\Int */
    public $groupId;

    .... getters and setter etc ....

}

MonitoringManager function to fetch all items,

public function getAllMonitoringItems(){

    return $this->dm->createQueryBuilder('MonitoringBundle:Monitoring')
                ->group(array(), array('groups' => array()))
                ->reduce('function (obj, prev) { 
                            var type = obj.type;
                            if(!prev.groups.hasOwnProperty(type)){
                                prev["groups"][type] = [];
                                prev["groups"][type].push(obj);
                            } else {
                                prev["groups"][type].push(obj);
                            }

                        }')
                ->field('type')->notIn(array("graph"))
                ->getQuery()
                ->execute()
                ->toArray();

}
Odyss3us
  • 6,457
  • 18
  • 74
  • 112
  • Did zou checked out the aggregation framework? see: http://docs.mongodb.org/manual/aggregation/ There is an aggregate() function in php aswell: http://php.net/manual/de/mongocollection.aggregate.php – Marc Mar 01 '13 at 09:00

0 Answers0