doule6314 2016-03-01 11:34
浏览 27

PHP - MONGODB - 查询中的变量

I have a problem with the use of PHP variables in MongoDB.

This query is ok:

$cursor1 = $collection->distinct(array('filename'=> array(['name'=>'pippo'])));

But this query does not work :

$cursor1 = $collection->distinct(array('filename'=> $whereClause));

where:

$whereClause = "array(['name'=>'pippo'])"

Why?

  • 写回答

1条回答 默认 最新

  • douang1243 2016-03-01 12:55
    关注

    From the docs, the distinct command returns a list of distinct values for the given key across a collection. In this case you want to give it two parameters i.e. the key to use and the query which you want to pass in with the variable.

    Consider the following example to demonstrate this

    <?php
    $m = new MongoClient("localhost");
    $collection = $m->selectDB("test")->selectCollection("Data");
    
    $collection->insert(array("filename" => "bar.txt", "name" => "bar"));
    $collection->insert(array("filename" => "foo", "name" => "pippo"));
    $collection->insert(array("filename" => "foo", "name" => "test"));
    
    $retval = $collection->distinct("filename");
    var_dump($retval);
    
    $whereClause = array("name" => "pippo");
    $retval = $collection->distinct("filename", $whereClause);
    var_dump($retval);
    
    ?>
    

    Another approach is using the command method to issue the distinct command. Consider the following mongo shell example which finds all the distinct values for the key "fieldname" from all the documents in the "Data" collection:

    db.runCommand({ distinct: "Data", key: "filename" })
    

    This returns a document with a field named values that contains the distinct filename values:

    {
       "values": [ "foo", "bar", "abc", "def" ],
       "stats": { ... },
       "ok" : 1
    }
    

    The next example returns the distinct values for the field filename from the "Data" collection with a query on documents whose name field is equal to "pippo":

    db.runCommand({ distinct: "Data", key: "filename", query: { name: "pippo"} })
    

    which produces a similar document as above:

    {
       "values": [ "abc", "def" ],
       "stats": { ... },
       "ok" : 1
    }
    

    The equivalent PHP implementation with iteration follows:

    <?php
    ...
    $whereClause = array("name" => "pippo");
    $filenames = $db->command(
        array(
            "distinct" => "Data",
            "key" => "filename", 
            "query" => $whereClause
        )
    );  
    
    foreach ($filenames['values'] as $filename) {
        $result1 = var_export($filename, true);
        echo "value2: ".$result1;
        $output[] = $result1;
        echo "</br>";
    }
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)