<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Runs JavaScript code on the database server.</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongodb.dropcollection.html">MongoDB::dropCollection</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongodb.forceerror.html">MongoDB::forceError</a></div> <div class="up"><a href="class.mongodb.html">MongoDB</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="mongodb.execute" class="refentry"> <div class="refnamediv"> <h1 class="refname">MongoDB::execute</h1> <p class="verinfo">(PECL mongo >=0.9.3)</p><p class="refpurpose"><span class="refname">MongoDB::execute</span> — <span class="dc-title">Runs JavaScript code on the database server.</span></p> </div> <div class="refsect1 description" id="refsect1-mongodb.execute-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type">array</span> <span class="methodname"><strong>MongoDB::execute</strong></span> ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$code</code></span> [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$args</code><span class="initializer"> = array()</span></span> ] )</div> <p class="para rdfs-comment"> 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. </p> <p class="para"> Running JavaScript in the database takes a write lock, meaning it blocks other operations. Make sure you consider this before running a long script. </p> <p class="para"> This is a wrapper for a database command. This method is basically: <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #007700">public function </span><span style="color: #0000BB">execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$code</span><span style="color: #007700">, </span><span style="color: #0000BB">$args</span><span style="color: #007700">) {<br /> return </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">command</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'$eval' </span><span style="color: #007700">=> </span><span style="color: #0000BB">$code</span><span style="color: #007700">, </span><span style="color: #DD0000">'args' </span><span style="color: #007700">=> </span><span style="color: #0000BB">$args</span><span style="color: #007700">));<br />}<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </p> <p class="para"> 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": </p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$db</span><span style="color: #007700">-></span><span style="color: #0000BB">execute</span><span style="color: #007700">(</span><span style="color: #DD0000">'"foo";'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <p class="para"> However, these return <strong><code>NULL</code></strong>: </p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$db</span><span style="color: #007700">-></span><span style="color: #0000BB">execute</span><span style="color: #007700">(</span><span style="color: #DD0000">'"bar"; "foo";'</span><span style="color: #007700">); </span><span style="color: #FF8000">// more than one statement<br /><br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">execute</span><span style="color: #007700">(</span><span style="color: #DD0000">'db.foo.count(<br />);'</span><span style="color: #007700">); </span><span style="color: #FF8000">// more than one line<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <p class="para"> 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: </p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$db</span><span style="color: #007700">-></span><span style="color: #0000BB">execute</span><span style="color: #007700">(</span><span style="color: #DD0000">'"bar"; return "foo";'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">execute</span><span style="color: #007700">(</span><span style="color: #DD0000">'return db.foo.count(<br />);'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <p class="para"> Now the first statement will return "foo" and the second statement will return a count of the "foo" collection. </p> </div> <div class="refsect1 parameters" id="refsect1-mongodb.execute-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"> <em><code class="parameter">code</code></em> </span> <dd> <p class="para"> <a href="class.mongocode.html" class="classname">MongoCode</a> or string to execute. </p> </dd> </dt> <dt> <span class="term"> <em><code class="parameter">args</code></em> </span> <dd> <p class="para"> Arguments to be passed to <em>code</em>. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-mongodb.execute-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the result of the evaluation. </p> </div> <div class="refsect1 examples" id="refsect1-mongodb.execute-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-1419"> <p><strong>Example #1 Simple <span class="function"><strong>MongoDB::execute()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$response </span><span style="color: #007700">= </span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">execute</span><span style="color: #007700">(</span><span style="color: #DD0000">"function() { return 'Hello, world!'; }"</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$response</span><span style="color: #007700">[</span><span style="color: #DD0000">'retval'</span><span style="color: #007700">];<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"><br /> Hello, world!<br /> </div> </div> <div class="example" id="example-1420"> <p><strong>Example #2 Parameter <span class="function"><strong>MongoDB::execute()</strong></span> example</strong></p> <div class="example-contents"><p> The optional array of parameters will be passed to the JavaScript function. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$response </span><span style="color: #007700">= </span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">execute</span><span style="color: #007700">(</span><span style="color: #DD0000">"function(greeting, name) { return greeting+', '+name+'!'; }"</span><span style="color: #007700">, array(</span><span style="color: #DD0000">"Good bye"</span><span style="color: #007700">, </span><span style="color: #DD0000">"Joe"</span><span style="color: #007700">));<br />echo </span><span style="color: #0000BB">$response</span><span style="color: #007700">[</span><span style="color: #DD0000">'retval'</span><span style="color: #007700">];<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"><br /> Good bye, Joe!<br /> </div> </div> <div class="example" id="example-1421"> <p><strong>Example #3 Scope example</strong></p> <div class="example-contents"><p> If a <a href="class.mongocode.html" class="classname">MongoCode</a> object is used instead of a string for the first parameter, a scope can be passed in which the JavaScript will be executed. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$func </span><span style="color: #007700">= <br /> </span><span style="color: #DD0000">"function(greeting, name) { "</span><span style="color: #007700">.<br /> </span><span style="color: #DD0000">"return greeting+', '+name+', says '+greeter;"</span><span style="color: #007700">.<br /> </span><span style="color: #DD0000">"}"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$scope </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"greeter" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"Fred"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$code </span><span style="color: #007700">= new </span><span style="color: #0000BB">MongoCode</span><span style="color: #007700">(</span><span style="color: #0000BB">$func</span><span style="color: #007700">, </span><span style="color: #0000BB">$scope</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$response </span><span style="color: #007700">= </span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$code</span><span style="color: #007700">, array(</span><span style="color: #DD0000">"Goodbye"</span><span style="color: #007700">, </span><span style="color: #DD0000">"Joe"</span><span style="color: #007700">));<br />echo </span><span style="color: #0000BB">$response</span><span style="color: #007700">[</span><span style="color: #DD0000">'retval'</span><span style="color: #007700">];<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"><br /> Goodbye, Joe, says Fred <br /> </div> </div> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongodb.dropcollection.html">MongoDB::dropCollection</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongodb.forceerror.html">MongoDB::forceError</a></div> <div class="up"><a href="class.mongodb.html">MongoDB</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>