PHP Doku:: Adds a top-level key/value pair to a query - mongocursor.addoption.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzDatenbankerweiterungenAnbieterspezifische DatenbankerweiterungenMongoDB Native DriverCore ClassesThe MongoCursor classMongoCursor::addOption

Ein Service von Reinhard Neidl - Webprogrammierung.

The MongoCursor class

<<The MongoCursor class

MongoCursor::batchSize>>

MongoCursor::addOption

(PECL mongo >=1.0.4)

MongoCursor::addOptionAdds a top-level key/value pair to a query

Beschreibung

public MongoCursor MongoCursor::addOption ( string $key , mixed $value )

This is an advanced function and should not be used unless you know what you're doing.

A query can optionally be nested in a "query" field if other options, such as a sort or hint, are given. For instance, adding a sort causes the query to become a subfield of a bigger query object, like:

<?php

$query 
= array("query" => $query"orderby" => $sort);

?>

This method is for adding a top-level field to a query. It makes the query a subobject (if it isn't already) and adds the key/value pair of your chosing to the top level.

Warnung

It cannot be used to add extra criteria to a query on the fly. For instance, this will not work:

<?php

// NOT CORRECT
$cursor $users->find()->addOption("name""joe")->addOption("age"20);

?>
This does not query for a user named "joe" with an age of 20.

Parameter-Liste

key

Fieldname to add.

value

Value to add.

Rückgabewerte

Returns this cursor.

Fehler/Exceptions

Throws MongoCursorException if this cursor has started iterating.

Beispiele

Beispiel #1 MongoCursor::addOption() example

Using MongoCursor::skip() to skip over millions of results can become slow. One way around this is to use $min or $max options for the query. These can be handy, but they require an index on exactly the fields being searched for. This is an example of how to use $min as an alternative to MongoCursor::skip().

<?php

// make sure we have an index
$c->ensureIndex(array("ts" => 1));

// you may have to modify this to run in a reasonable amount of time on slow 
// machines (should take about 30 seconds on a good machine)
for ($i 0$i 30000000$i++) {
    
$c->insert(array("ts" => new MongoDate(), "i" => $i));
}

$now strtotime("now");

// find documents inserted in the last 2 seconds
$cursor $c->find()->addOption('$min', array("ts" => $now-2));

?>

Keine BenutzerBeiträge.
- Beiträge aktualisieren...



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)