【问题标题】:can i pass mongodb query as a string in php我可以在 php 中将 mongodb 查询作为字符串传递吗
【发布时间】:2017-01-13 01:37:56
【问题描述】:

我打算从 php 平台文本区域运行 mongodb 查询,如下图所示,我想在其中编写查询,如

 array('Chat_time' => array('$gt' => $start, '$lte' => $end))

然后像这样执行

$m = new MongoClient();
$db = $m->Forensic;
$coll= $db->mobile_
$user_code = $coll->find($_POST['txt_area']));

但无法执行,因为我认为当我将查询转换为字符串时,它无法将 => 理解为命令。

将这个命令作为字符串传递的最佳方式是什么,php 会理解。

【问题讨论】:

    标签: php json mongodb


    【解决方案1】:

    您应该将 json 写入您的文本区域。喜欢:

    {
        "Chat_time": {
            "$gt" => "xxx",
            "$lte" => "yyy"
        }
    }
    

    在php中

    // json string to array using json_decode
    $query = json_decode($_POST['txt_area'], true);
    $user_code = $coll->find($query);
    

    【讨论】:

    • 感谢您的回答,但并未真正得到任何答复。出现以下错误消息 注意:未定义变量:第 157 行 C:\xampp\htdocs\reports3.php 中的 p 警告:MongoCollection::find(): 期望参数 1 是数组或对象,C:\xampp 中给出的字符串\htdocs\reports3.php 第 159 行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 2012-02-07
    • 1970-01-01
    • 2015-04-17
    • 1970-01-01
    相关资源
    最近更新 更多