3

I ran this command in rockmongo tools- command and its returning correct values

{ 
    aggregate : "twits",                                
    pipeline : [
         {$group : {_id : "$by", num_tutorial : {$sum : 1}}}
    ]                
}

and then I tried to extract the values from it using this

 define("__COLLECTION_TWITS__","twits");

in /var/www/html/TradeTwits/require/config/DBConfig.inc.php

$collectionName=__COLLECTION_TWITS__;
$db = $this->connection("mo");
$col = $db->$collectionName;

return $col->aggregate(['$group'=> array('_id' => '$by','num_tutorial' => array($sum => 1))]); 

in /var/www/html/TradeTwits/app/models/BaseModel.php

The above code should be returning all records grouped by usernames, but when in php ,it is not working fine.

Am using phalcon framework using PECL class library to access mongodb and rockmongo as GUI for accessing mongodb collection

It is returning correct values for find() count() etc.Should I use another library for aggregate or pipeline?

1
  • $sum should have simple quotes. Commented Dec 11, 2015 at 9:02

1 Answer 1

3

Aggregate should be an array of arrays. I think here, you missed an array.

The documentation says:

$ops = array(
    array(
        '$project' => array(
            "author" => 1,
            "tags"   => 1,
        )
    ),
    array('$unwind' => '$tags'),
    array(
        '$group' => array(
            "_id" => array("tags" => '$tags'),
            "authors" => array('$addToSet' => '$author'),
        ),
    ),
);
$results = $c->aggregate($ops);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! :) A little bug was ruining it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.