1

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?

3
  • 1
    YOu are passing in a string instead of an array. Commented Mar 1, 2016 at 11:38
  • In your case $whereClause is a string from what i see, you surround the value with double quotes. Remove the double quotes and let $whereClause be an php array. Commented Mar 1, 2016 at 11:39
  • Thanks for the answer. When i remove the double quote i receive this error : Warning: MongoCollection::distinct() expects parameter 1 to be string, array given in C:\wamp\www\prove\selectMongoDB.php on line 44 Commented Mar 1, 2016 at 11:45

1 Answer 1

1

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>";
}

?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.