$ concat mongo不工作

时间:2016-07-07 11:41:04

标签: php mongodb laravel

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

// code based on Richard Hodges example

template<class Handler>
void long_process_with_completion_handler(Handler done) {
    std::thread([done] {
        std::cout << "long process starts" << std::endl;
        std::this_thread::sleep_for(2000ms);
        done();
    }).detach();
}

// without the need to inherit from std::enable_shared_from_this
struct Controller {
    auto get_lock() const {
        return std::unique_lock<std::mutex>(_mutex);
    }
    static void start(std::shared_ptr<Controller>& pcontroller) {
        long_process_with_completion_handler(
        [self = std::shared_ptr<Controller>(pcontroller)] {
            auto lock = self->get_lock();
            std::cout << "all complete" << std::endl;
        });
    }
    mutable std::mutex _mutex;
};

int main() {
    std::condition_variable controller_done;
    std::mutex done_mutex;
    bool is_controller_done = 0;
    // make shared controller and start its processing
    auto pcontroller = std::shared_ptr<Controller>{ new Controller,
        [&](auto*p) {
            delete p;
            auto lock = std::unique_lock<std::mutex>(done_mutex);
            is_controller_done = true;
            std::cout << "controller destroyed" << std::endl;
            lock.unlock();
            controller_done.notify_all();
    }};
    Controller::start(pcontroller);

    // destroy the controlling pointer. but our controller is still running...
    pcontroller.reset();

    auto lock = std::unique_lock<std::mutex>(done_mutex);
    controller_done.wait(lock, [&]{ return is_controller_done;});
    std::cout << "program ends" << std::endl;
}

将输出作为例外

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

2 个答案:

答案 0 :(得分:0)

你应该使用双引号:

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

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

答案 1 :(得分:0)

后面是正确的语法,例如:

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