Geen cache-versie.


Caching uitgeschakeld. Standaardinstelling voor deze pagina:ingeschakeld (code LNG204)
Als het scherm te langzaam is, kunt u de gebruikersmodus uitschakelen om de cacheversie te bekijken.

Rechercher une fonction PHP

MongoCollection::findAndModify

(PECL mongo >=1.3.0)

MongoCollection::findAndModifyUpdate a document and return it

Description

public MongoCollection::findAndModify ( array $query [, array $update [, array $fields [, array $options ]]] ) : array

The findAndModify command atomically modifies and returns a single document. By default, the returned document does not include the modifications made on the update. To return the document with the modifications made on the update, use the new option.

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Parameters

query

The query criteria to search for.

update

The update criteria.

fields

Optionally only return these fields.

options

An array of options to apply, such as remove the match document from the DB and return it.

Option Description
sort array Determines which document the operation will modify if the query selects multiple documents. findAndModify will modify the first document in the sort order specified by this argument.
remove boolean Optional if update field exists. When TRUE, removes the selected document. The default is FALSE.
update array Optional if remove field exists. Performs an update of the selected document.
new boolean Optional. When TRUE, returns the modified document rather than the original. The findAndModify method ignores the new option for remove operations. The default is FALSE.
upsert boolean Optional. Used in conjunction with the update field. When TRUE, the findAndModify command creates a new document if the query returns no documents. The default is false. In MongoDB 2.2, the findAndModify command returns NULL when upsert is TRUE.

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Return Values

Returns the original document, or the modified document when new is set.

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Examples

Example #1 MongoCollection::findAndModify() example

<?php
$m 
= new Mongo;
$col $m->selectDB("test")->jobs;

$col->insert(array(
     
"name" => "Next promo",
     
"inprogress" => false,
     
"priority" => 0,
     
"tasks" => array( "select product""add inventory""do placement"),
) );

$col->insert(array(
     
"name" => "Biz report",
     
"inprogress" => false,
     
"priority" => 1,
     
"tasks" => array( "run sales report""email report" )
) );

$col->insert(array(
     
"name" => "Biz report",
     
"inprogress" => false,
     
"priority" => 2,
     
"tasks" => array( "run marketing report""email report" )
    ),
    array(
"w" => 1)
);



$retval $col->findAndModify(
     array(
"inprogress" => false"name" => "Biz report"),
     array(
'$set' => array('inprogress' => true"started" => new MongoDate())),
     
null,
     array(
        
"sort" => array("priority" => -1),
        
"new" => true,
    )
);

var_dump($retval);
?>

The above example will output something similar to:

array(6) {
  ["_id"]=>
  object(MongoId)#7 (1) {
    ["$id"]=>
    string(24) "5091b5b244415e8cc3000002"
  }
  ["inprogress"]=>
  bool(true)
  ["name"]=>
  string(10) "Biz report"
  ["priority"]=>
  int(2)
  ["started"]=>
  object(MongoDate)#8 (2) {
    ["sec"]=>
    int(1351726514)
    ["usec"]=>
    int(925000)
  }
  ["tasks"]=>
  array(2) {
    [0]=>
    string(20) "run marketing report"
    [1]=>
    string(12) "email report"
  }
}

Example #2 MongoCollection::findAndModify() error handling

<?php
$m 
= new Mongo;
$col $m->selectDB("test")->jobs;
try {
    
$retval $col->findAndModify(
         array(
"inprogress" => false"name" => "Next promo"),
         array(
'$pop' => array("tasks" => -1)),
         array(
"tasks" => array('$pop' => array("stuff"))),
         array(
"new" => true)
    );
} catch(
MongoResultException $e) {
    echo 
$e->getCode(), " : "$e->getMessage(), "\n";
    
var_dump($e->getDocument());
}

?>

The above example will output something similar to:

13097 : exception: Unsupported projection option: $pop
array(3) {
  ["errmsg"]=>
  string(46) "exception: Unsupported projection option: $pop"
  ["code"]=>
  int(13097)
  ["ok"]=>
  float(0)
}
Zoek een PHP-functie

Vertaling niet beschikbaar

De PHP-handleiding is nog niet in het Nederlands vertaald, dus het scherm is in het Engels. Als u wilt, kunt u het ook in het Frans of in het Duits raadplegen.

Als je de moed voelt, kun je je vertaling aanbieden ;-)

Nederlandse vertaling

U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.

Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.

Bij voorbaat dank.

Document heeft de 30/01/2003 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/php-rf-mongocollection.findandmodify.html

De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.

Referenties

  1. Bekijk - html-document Taal van het document:fr Manuel PHP : http://php.net

Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.

Inhoudsopgave Haut