Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search in MongoDB array using PHP

I have some mongoDB documents with the following structure:

[_id] => MongoId Object (
  [$id] => 50664339b3e7a7cf1c000001
)
[uid] => 1
  [name] => Alice
  [words] => 1
  [formatIds] => Array (
        [0] => 1
        [1] => 4
  )

What I want to do is find all documents which has the value 1 in formatIds[ ]. I think it's possible to accomplish that. How can I do it in PHP?

UPDATE

Thanks for the help. It works fine now. Here is how i wrote the search,

$id=$_POST['id'];
$query = array('formatIds'=> "{$id}" );
$result = $stations_table->find($query); //where $stations_table = $db->stations;
like image 814
Manu Avatar asked May 22 '26 07:05

Manu


1 Answers

MongoDB treats queries on array values the same way as queries on standard values, as per the docs.

Querying for array('formatIds' => 1) should work.

like image 176
shelman Avatar answered May 23 '26 20:05

shelman