The MongoDB\Driver\Query class

(mongodb >=1.0.0)

¼ò½é

The MongoDB\Driver\Query class is a value object that represents a database query.

ÀàÕªÒª

final MongoDB\Driver\Query {
/* ·½·¨ */
final public __construct ( array|object $filter [, array $queryOptions ] )
}

Table of Contents

User Contributed Notes

dvdogrady at gmail dot com 30-Oct-2017 12:58
Hello

Im just writing this note to help people out who want to write $filter arrays with query operators in them.

I have copied this code off maha88a's note but I changed the $filter to give you an idea of how it would work with the query operators

Considering the following MangoDB collection:

<?php
/* my_collection */

/* 1 */
{
   
"_id" : ObjectId("5707f007639a94cbc600f282"),
   
"id" : 1,
   
"name" : "Name 1"
}

/* 2 */
{
   
"_id" : ObjectId("5707f0a8639a94f4cd2c84b1"),
   
"id" : 2,
   
"name" : "Name 2"
}
?>

I'm using the following code:
<?php
// This $filter will return any id's qualing to 2 but what if we want all the id's above 0.
// $filter = ['id' => 2];
// This is how we would do this.
$filter = ['id' => ['$gt' => 0]]
$options = [
  
'projection' => ['_id' => 0],
];
$query = new MongoDB\Driver\Query($filter, $options);
$rows = $mongo->executeQuery('db_name.my_collection', $query); // $mongo contains the connection object to MongoDB
foreach($rows as $r){
  
print_($r);
}
?>
yatiny3007 at gmail dot com 04-Feb-2017 04:03
Find By _id

$mongo = new \MongoDB\Driver\Manager('mongodb://root:root@192.168.0.127/db');

$id           = new \MongoDB\BSON\ObjectId("588c78ce02ac660426003d87");
$filter      = ['_id' => $id];
$options = [];

$query = new \MongoDB\Driver\Query($filter, $options);
$rows   = $mongo->executeQuery('db.collectionName', $query);

foreach ($rows as $document) {
  pr($document);
}
Dalahimself 18-Nov-2016 04:12
How to query for an MongoDb _id with value $id

<?php
$filter
=array();
$filter['_id']=new MongoDB\BSON\ObjectID($id);
               
$command=[
                 
'find' => 'my_collection',
                 
'filter' => $filter
];
               
$query = new MongoDB\Driver\Command($command);
?>
maha88a 10-Apr-2016 02:38
Here is an example of Query to retrieve the records from MangoDB collection using a filter. It will return in this case just one record that satisfy the filter id = 2.

Considering the following MangoDB collection:

<?php
/* my_collection */

/* 1 */
{
   
"_id" : ObjectId("5707f007639a94cbc600f282"),
   
"id" : 1,
   
"name" : "Name 1"
}

/* 2 */
{
   
"_id" : ObjectId("5707f0a8639a94f4cd2c84b1"),
   
"id" : 2,
   
"name" : "Name 2"
}
?>

I'm using the following code:
<?php
$filter
= ['id' => 2];
$options = [
  
'projection' => ['_id' => 0],
];
$query = new MongoDB\Driver\Query($filter, $options);
$rows = $mongo->executeQuery('db_name.my_collection', $query); // $mongo contains the connection object to MongoDB
foreach($rows as $r){
  
print_($r);
}
?>
andzandz 15-Mar-2016 09:10
Careful with your filter here - integers and strings are different.

With a string ID '1000', this query will mysteriously return 0 results; wasted time figuring out what was going on:

$query = new MongoDB\Driver\Query(['_id' => 1000]);

This will work:

$query = new MongoDB\Driver\Query(['_id' => '1000']);

Would be nice if the library could handle this.