0

Using $project operator and want to concatenate two fields result and one string to them.

  'check_concat'=>array('$concat'=>'$pickup_delivery_date_and_time_to','cheema'),

giving output as an exception

127.0.0.1:27017: exception: this object is already an operator expression, and can't be used as a document expression (at '0')
Pankaj Cheema
  • 1,028
  • 2
  • 13
  • 26
  • This may help you with the exact notations to used.https://docs.mongodb.com/manual/reference/operator/aggregation/project/ – Dileephell Jul 07 '16 at 11:45

2 Answers2

0

You should use double quotes:

'check_concat' => array("$concat"=>"$pickup_delivery_date_and_time_to",'cheema'),

What is the difference between single-quoted and double-quoted strings in PHP?

Community
  • 1
  • 1
Maksim I. Kuzmin
  • 1,170
  • 7
  • 16
0

The correct syntax follows, for example:

db.collection.aggregate([
    { 
        "$project": { 
            "check_concat": { 
                "$concat": ["$pickup_delivery_date_and_time_to", "cheema"] 
            } 
        } 
    }
])

in PHP, this translate to

<?php
$m = new MongoClient("localhost");
$c = $m->selectDB("test")->selectCollection("collection");
$ops = array(
    array(
        "$project" => array(
            "check_concat" => array(                
                "$concat" => array("$pickup_delivery_date_and_time_to", "cheema")
            )
        )
    )
);
$results = $c->aggregate($ops);
var_dump($results);
?>
chridam
  • 100,957
  • 23
  • 236
  • 235