略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: MongoDB\Driver\Query

2024-11-13

The MongoDB\Driver\Query class

(mongodb >=1.0.0)

简介

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

类摘要

final class MongoDB\Driver\Query {
/* 方法 */
final public __construct(array|object $filter, array $queryOptions = ?)
}

目录

add a noteadd a note

User Contributed Notes 7 notes

up
1
jaimegarbanzo at gmail dot com
1 year ago
# constructing a to a remote server and querry and limiting the result to the latest 3 results

$username ="yourusername";
$password = "yourpasswort";

$manager = new MongoDB\Driver\Manager("mongodb://yourserver.com/",
     array("username" => $username, "password" => $password )
    );   

# setting your options and filter
$filter  = [];
$options = ['sort'=>array('_id'=>-1),'limit'=>3]; # limit -1 from newest to oldest

#constructing the querry
$query = new MongoDB\Driver\Query($filter, $options);

#executing
$cursor = $manager->executeQuery('resultdb.test', $query);

echo "dumping results<br>";
foreach ($cursor as $document) {
    var_dump($document);
}
up
16
maha88a
6 years ago
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);
}
?>
up
4
tiwaritusharudayan at gmail dot com
4 years ago
This format displays the documents returned from the query as stdClass Objects. For display purposes, it's fine, but if certain computations need to be performed using the returned documents and their corresponding fields, use this:

------------------------------------Code Snippet-----------------------------------

$connection = new MongoDB\Driver\Manager("mongodb://localhost:27017");
   
$filter = ["name"=>"insert_desired_name_here"];
/* the following condition, and others similar to it, work as well

$filter = ["age"=>["$gt"=>"18"]]; /*/
   
$options = []; /* put desired options here, should you need any */
   
$query = new MongoDB\Driver\Query($filter,$options);
   
$documents = $connection->executeQuery('trial.trial' /*dbname.collection_name*/,$query);
   
foreach($documents as $document){
    $document = json_decode(json_encode($document),true);
    echo $document[name];
}

--------------------------------------------------------------------------------------

The first executable statement inside the foreach encodes the returned stdClass object to a json object, and the same is immediately decoded to an associative array, which can then be used as and when needed
up
11
yatiny3007 at gmail dot com
5 years ago
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);
}
up
4
dvdogrady at gmail dot com
4 years ago
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);
}
?>
up
-28
Dalahimself
5 years ago
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);
?>
up
-62
andzandz
6 years ago
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.

官方地址:https://www.php.net/manual/en/class.mongodb-driver-query.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3