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

MongoDB::execute

(PECL mongo >=0.9.3)

MongoDB::executeRuns JavaScript code on the database server [deprecated]

Description

public MongoDB::execute ( mixed $code [, array $args = array() ] ) : array
Warning

The » eval command, which this method invokes, is deprecated in MongoDB 3.0+.

The Mongo database server runs a JavaScript engine. This method allows you to run arbitary JavaScript on the database. This can be useful if you want touch a number of collections lightly, or process some results on the database side to reduce the amount that has to be sent to the client.

Running JavaScript in the database takes a write lock, meaning it blocks other operations. Make sure you consider this before running a long script.

This is a wrapper for the » eval database command. This method is basically:

<?php

public function execute($code$args) {
    return 
$this->command(array('eval' => $code'args' => $args));
}

?>

MongoDB implies a return statement if you have a single statement on a single line. This can cause some unintuitive behavior. For example, this returns "foo":

<?php

$db
->execute('"foo";');

?>

However, these return NULL:

<?php

$db
->execute('"bar"; "foo";'); // more than one statement

$db->execute('db.foo.count(
);'
); // more than one line

?>

To avoid surprising behavior, it is best not to depend on MongoDB to decide what to return, but to explicitly state a return value. In the examples above, we can change them to:

<?php

$db
->execute('"bar"; return "foo";');

$db->execute('return db.foo.count(
);'
);

?>

Now the first statement will return "foo" and the second statement will return a count of the "foo" collection.

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Parameters

code

MongoCode or string to execute.

args

Arguments to be passed to code.

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Return Values

Returns the result of the evaluation.

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Examples

Example #1 Simple MongoDB::execute() example

<?php

$response 
$db->execute("function() { return 'Hello, world!'; }");
echo 
$response['retval'];

?>

The above example will output something similar to:


Hello, world!

Example #2 Parameter MongoDB::execute() example

The optional array of parameters will be passed to the JavaScript function.

<?php

$response 
$db->execute("function(greeting, name) { return greeting+', '+name+'!'; }", array("Good bye""Joe"));
echo 
$response['retval'];

?>

The above example will output something similar to:


Good bye, Joe!

Example #3 Scope example

If a MongoCode object is used instead of a string for the first parameter, a scope can be passed in which the JavaScript will be executed.

<?php

$func 

    
"function(greeting, name) { ".
        
"return greeting+', '+name+', says '+greeter;".
    
"}";
$scope = array("greeter" => "Fred");

$code = new MongoCode($func$scope);

$response $db->execute($code, array("Goodbye""Joe"));
echo 
$response['retval'];

?>

The above example will output something similar to:


Goodbye, Joe, says Fred

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Changelog

Version Description
1.7.0 This method has been deprecated as a result of the underlaying » eval command being deprecated in MongoDB 3.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-mongodb.execute.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