(mongodb >=1.0.0)
MongoDB\Driver\Query::__construct — Create a new Query
Constructs a new MongoDB\Driver\Query, which is an immutable value object that represents a database query. The query may then be executed with MongoDB\Driver\Manager::executeQuery().
filter
(array|object)Das » Abfrage-Prädikat. Ein leeres Prädikat passt auf alle Dokumente in der Sammlung.
Hinweis: Bei der Auswertung von Abfragekriterien vergleicht MongoDB Typen und Werte gemäß seiner eigenen » Vergleichsregeln für BSON-Typen, die sich von PHPs Regeln für Typenvergleich und Typen-Jonglage unterscheidet. Wenn es eine Übereinstimmung mit einem speziellen BSON-Typ gibt, sollten die Abfragekriterien die entsprechende BSON-Klasse verwenden (z. B. MongoDB\BSON\ObjectId, um auf eine » ObjectId zu passen).
queryOptions
Option | Type | Description |
---|---|---|
allowDiskUse | bool |
Allows MongoDB to use temporary disk files to store data exceeding the 100 megabyte system memory limit while processing a blocking sort operation. |
allowPartialResults | bool |
For queries against a sharded collection, returns partial results from the mongos if some shards are unavailable instead of throwing an error.
Falls back to the deprecated |
awaitData | bool |
Use in conjunction with the "tailable" option to
block a getMore operation on the cursor temporarily if at the end of
data rather than returning no data. After a timeout period, the query
returns as normal.
|
batchSize | int |
The number of documents to return in the first batch. Defaults to 101. A batch size of 0 means that the cursor will be established, but no documents will be returned in the first batch. In versions of MongoDB before 3.2, where queries use the legacy wire protocol OP_QUERY, a batch size of 1 will close the cursor irrespective of the number of matched documents. |
collation | array|object |
Die » Collation (Vergleichs-/Sortierregel) ermöglicht es dem Benutzer, sprachspezifische Regeln für den Vergleich von Zeichenketten anzugeben, so z. B. Regeln für die Groß- und Kleinschreibung und Akzentzeichen. Bei der Angabe der Sortierung ist das Feld Wenn keine Kollation angegeben wurde, aber die Sammlung eine Standardkollation hat, verwendet die Operation die für die Sammlung angegebene Kollation. Wenn weder für die Sammlung noch für die Operation eine Kollation angegeben wurde, verwendet MongoDB den einfachen binären Vergleich, der in früheren Versionen für den Vergleich von Zeichenketten verwendet wurde. Diese Option ist ab MongoDB 3.4 verfügbar. Wenn sie bei einer älteren Serverversion angegeben wird, kommt es bei der Ausführung zu einer Exception. |
comment | mixed |
An arbitrary comment to help trace the operation through the database profiler, currentOp output, and logs. The comment can be any valid BSON type for MongoDB 4.4+. Earlier server versions only support string values.
Falls back to the deprecated |
exhaust | bool |
Stream the data down full blast in multiple "more" packages, on the assumption that the client will fully read all data queried. Faster when you are pulling a lot of data and know you want to pull it all down. Note: the client is not allowed to not read all the data unless it closes the connection. This option is not supported by the find command in MongoDB 3.2+ and will force the driver to use the legacy wire protocol version (i.e. OP_QUERY). |
explain | bool |
If
Falls back to the deprecated This option is not supported by the find command in MongoDB 3.2+ and will only be respected when using the legacy wire protocol version (i.e. OP_QUERY). The » explain command should be used on MongoDB 3.0+. |
hint | string|array|object |
Index specification. Specify either the index name as a string or the index key pattern. If specified, then the query system will only consider plans using the hinted index.
Falls back to the deprecated |
let | array|object |
Die Zuordnung von Parameternamen und -werten. Die Werte müssen Konstanten oder geschlossene Ausdrücke sein, die nicht auf Dokumentfelder verweisen. Auf die Parameter kann dann als Variablen im Kontext eines aggregierten Ausdrucks zugegriffen werden (z. B. Diese Option ist ab MongoDB 5.0 verfügbar. Wenn sie bei einer älteren Serverversion angegeben wird, kommt es bei der Ausführung zu einer Exception. |
limit | int |
The maximum number of documents to return. If unspecified, then defaults to no limit. A limit of 0 is equivalent to setting no limit.
A negative limit is will be interpreted as a positive limit with the
|
max | array|object |
The exclusive upper bound for a specific index.
Falls back to the deprecated |
maxAwaitTimeMS | int |
Positive integer denoting the time limit in milliseconds for the
server to block a getMore operation if no data is available. This
option should only be used in conjunction with the
|
maxScan | int |
Warnung
This option is deprecated and should not be used. Positive integer denoting the maximum number of documents or index keys to scan when executing the query.
Falls back to the deprecated |
maxTimeMS | int |
The cumulative time limit in milliseconds for processing operations on the cursor. MongoDB aborts the operation at the earliest following interrupt point.
Falls back to the deprecated |
min | array|object |
The inclusive lower bound for a specific index.
Falls back to the deprecated |
modifiers | array | » Meta operators modifying the output or behavior of a query. Use of these operators is deprecated in favor of named options. |
noCursorTimeout | bool | Prevents the server from timing out idle cursors after an inactivity period (10 minutes). |
oplogReplay | bool |
Internal use for replica sets. To use oplogReplay, you must include the following condition in the filter:
[ 'ts' => [ '$gte' => <timestamp> ] ]
|
projection | array|object |
The » projection specification to determine which fields to include in the returned documents. If you are using the ODM functionality to deserialise documents as their original PHP class, make sure that you include the __pclass field in the projection. This is required for the deserialization to work and without it, the extension will return (by default) a stdClass object instead. |
readConcern | MongoDB\Driver\ReadConcern |
A read concern to apply to the operation. By default, the read concern from the MongoDB Connection URI will be used. This option is available in MongoDB 3.2+ and will result in an exception at execution time if specified for an older server version. |
returnKey | bool |
If
Falls back to the deprecated |
showRecordId | bool |
Determines whether to return the record identifier for each
document. If
Falls back to the deprecated |
singleBatch | bool |
Determines whether to close the cursor after the first batch.
Defaults to false .
|
skip | int | Number of documents to skip. Defaults to 0. |
snapshot | bool |
Warnung
This option is deprecated and should not be used. Prevents the cursor from returning a document more than once because of an intervening write operation.
Falls back to the deprecated |
sort | array|object |
The sort specification for the ordering of the results.
Falls back to the deprecated |
tailable | bool | Returns a tailable cursor for a capped collection. |
Version | Beschreibung |
---|---|
PECL mongodb 1.14.0 |
Added the |
PECL mongodb 1.8.0 |
Added the
The |
PECL mongodb 1.5.0 |
The |
PECL mongodb 1.3.0 |
Added the |
PECL mongodb 1.2.0 |
Added the
Renamed the
Removed the legacy |
PECL mongodb 1.1.0 | Added the "readConcern" option. |
Beispiel #1 MongoDB\Driver\Query::__construct() example
<?php
/* Select only documents authord by "bjori" with at least 100 views */
$filter = [
'author' => 'bjori',
'views' => [
'$gte' => 100,
],
];
$options = [
/* Only return the following fields in the matching documents */
'projection' => [
'title' => 1,
'article' => 1,
],
/* Return the documents in descending order of views */
'sort' => [
'views' => -1
],
];
$query = new MongoDB\Driver\Query($filter, $options);
$manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');
$readPreference = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::PRIMARY);
$cursor = $manager->executeQuery('databaseName.collectionName', $query, $readPreference);
foreach($cursor as $document) {
var_dump($document);
}
?>