LeWestopher / cakephp-monga

A CakePHP 3.x plugin for accessing MongoDB NoSQL data stores
MIT License
17 stars 3 forks source link

How can i use aggregate function #7

Closed basantk closed 7 years ago

basantk commented 7 years ago

I have GroupsCollection extended BaseCollection and i have to select data by using aggregate function like in BaseCollection $this->aggregate([]); //getting all record

$this->aggregate(['group'=>['name']]); //getting error please help me

LeWestopher commented 7 years ago

Hey @basantk

Would you mind sharing the error that is being thrown? I'd be happy to be of assistance.

WK

basantk commented 7 years ago

I am using $data = $this->aggregate(['group'=>['name']]); pr($data);

error : localhost:27017: exception: Unrecognized pipeline stage name: 'group'

basantk commented 7 years ago
<?php
namespace App\Model\MongoCollection;

use CakeMonga\MongoCollection\BaseCollection;

use League\Monga\Collection;

class GroupsCollection extends BaseCollection
{
    public function findData(){
     $data = $this->aggregate(['group'=>['name']]);
     pr($data);
    }
}

error:   localhost:27017: exception: Unrecognized pipeline stage name: 'group' 
basantk commented 7 years ago

Can I use lookup

$ops1 = array( array( '$lookup' => array( "from" => "users", "localField" => "title", "foreignField" => "title", "as" => "user_docs" ), ) );

getting : localhost:27017: exception: Unrecognized pipeline stage name: '$lookup'

LeWestopher commented 7 years ago

Closing this issue, as this issue is outside the scope of this plugin. $this->aggregate() is a direct wrapped method from the Monga, a PHP library provided as a data layer for MongoDB, a library that this plugin uses to access MongoDB. I would suggest checking out the Monga docs to fully understand what's happening under the hood for all of the methods that it has. All of the data methods on cakephp-monga's Collection class are direct calls to the same methods from the Monga API. Here are the docs for Monga:

https://github.com/thephpleague/monga

In the meantime, I might be able to address the issue myself. Try this:

$collection->aggregate(function ($a) {
    $a->group(function ($g) {
        $g->by(['name']);
    });
});