No cache version.

Caching disabled. Default setting for this page:enabled (code LNG204)
If the display is too slow, you can disable the user mode to view the cached version.

Rechercher une fonction PHP

MongoCursor::addOption

(PECL mongo >=1.0.4)

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

Description

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

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.

Warning

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.

PHP: MongoCursor::addOption - Manual Home of Manuel PHP  Contents Haut

Parameters

key

Fieldname to add.

value

Value to add.

PHP: MongoCursor::addOption - Manual Home of Manuel PHP  Contents Haut

Errors/Exceptions

Throws MongoCursorException if this cursor has started iterating.

PHP: MongoCursor::addOption - Manual Home of Manuel PHP  Contents Haut

Examples

Example #1 Adding a comment with MongoCursor::addOption() example

MongoDB supports special options to be send to the server. The shell uses the _addSpecial option to send a $comment to the server. This comment will show up in the profiling log (for slow queries f.e.). In the PHP driver, you use the MongoCursor::addOption() method.

<?php
$m 
= new MongoClient;
$c $m->demo->demo;
$cursor $c->find();
$cursor->addOption('$comment'"This comment will show up in the profiling log");

foreach (
$cursor as $document) { /* empty */ }
?>

The above example will output something similar to:

{
    "op" : "query",
    "ns" : "demo.demo",
    "query" : {
        "$query" : {
             
        },
        "$comment" : "This comment will show up in the profiling log"
    },
    "cursorid" : 168463566447,
    "ntoreturn" : 0,
    "ntoskip" : 0,
    "nscanned" : 101,
    "nscannedObjects" : 101,
    "keyUpdates" : 0,
    "numYield" : 0,
…

Example #2 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));

?>
Find a PHP function

English translation

You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.

If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.

Thank you in advance.

Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-mongocursor.addoption.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.

References

  1. View the html document Language of the document:fr Manuel PHP : http://php.net

These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.

Contents Haut