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

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.

PHP: MongoDB::execute - Manual Home of Manuel PHP  Contents Haut

Parameters

code

MongoCode or string to execute.

args

Arguments to be passed to code.

PHP: MongoDB::execute - Manual Home of Manuel PHP  Contents Haut

Return Values

Returns the result of the evaluation.

PHP: MongoDB::execute - Manual Home of Manuel PHP  Contents 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

PHP: MongoDB::execute - Manual Home of Manuel PHP  Contents 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+.

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-mongodb.execute.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