0

I'm getting an error when I'm trying to execute a MongoDB query in PHP.

Parse error: syntax error, unexpected T_DOUBLE_ARROW in fiename.php on line 55

$findCount = array("$group" => (_id => "$createdby", count => ($sum:1))); //Line 55
$cursor = $collection->aggregate($findCount);

I went through most of the previously asked questions but couldn't arrive to a solution.

By the way, this is my query in MongoDB

db.collection_name.aggregate([{"$group" : {_id:"$createdby", count:{$sum:1}}} ])

Thanks

chridam
  • 100,957
  • 23
  • 236
  • 235
Anubhav
  • 147
  • 1
  • 13

1 Answers1

0

Your pipeline is missing some elements notably the array syntax. The correct syntax should be:

$findCount = array(
    array(
        "$group" => array(
            "_id" => "$createdby", 
            "count" => array("$sum" => 1)
        )
    )
);      
$cursor = $collection->aggregate($findCount);
chridam
  • 100,957
  • 23
  • 236
  • 235
  • Thanks for replying. It solved my problem but now I'm getting `Fatal error: Uncaught exception 'MongoException' with message 'zero-length keys are not allowed, did you use $ with double quotes?' in filename.php:63 Stack trace: #0 filename.php(63): MongoCollection->aggregate(Array) #1 {main} thrown in filename.php on line 63` – Anubhav Nov 14 '16 at 09:24
  • Check the answers in this [question](http://stackoverflow.com/questions/17024593) – chridam Nov 14 '16 at 09:28
  • 1
    I replaced double quotes with single quotes and it worked. Guess php was recognising them as variables earlier. Thank you. :) – Anubhav Nov 14 '16 at 10:40