$concat蒙戈不起作用


$concat mongo not working

使用$project运算符,并希望将两个字段结果和一个字符串连接到它们。

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

将输出作为例外提供

127.0.0.1:27017: exception: this object is already an operator expression, and can't be used as a document expression (at '0')

你应该使用双引号:

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

PHP 中的单引号和双引号字符串有什么区别?

正确的语法如下,例如:

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

在 PHP 中,这转化为

<?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);
?>