<!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>Execute a database command on this server</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongodb-driver-server.executebulkwrite.html">MongoDB\Driver\Server::executeBulkWrite</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongodb-driver-server.executequery.html">MongoDB\Driver\Server::executeQuery</a></div> <div class="up"><a href="class.mongodb-driver-server.html">MongoDB\Driver\Server</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="mongodb-driver-server.executecommand" class="refentry"> <div class="refnamediv"> <h1 class="refname">MongoDB\Driver\Server::executeCommand</h1> <p class="verinfo">(mongodb >=1.0.0)</p><p class="refpurpose"><span class="refname">MongoDB\Driver\Server::executeCommand</span> — <span class="dc-title">Execute a database command on this server</span></p> </div> <div class="refsect1 description" id="refsect1-mongodb-driver-server.executecommand-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">final</span> <span class="modifier">public</span> <span class="type"><span class="type MongoDB\Driver\Cursor">MongoDB\Driver\Cursor</span></span> <span class="methodname"><strong>MongoDB\Driver\Server::executeCommand</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$db</code></span> , <span class="methodparam"><span class="type"><span class="type MongoDB\Driver\Command">MongoDB\Driver\Command</span></span> <code class="parameter">$command</code></span> [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$options</code><span class="initializer"> = array()</span></span> ] )</div> <p class="para rdfs-comment"> Executes the command on this server. </p> <p class="para"> This method applies no special logic to the command. Although this method accepts <em>"readConcern"</em> and <em>"writeConcern"</em> options, which will be incorporated into the command document, those options will not default to corresponding values from the <a href="mongodb-driver-manager.construct.html#mongodb-driver-manager.construct-uri" class="link">MongoDB Connection URI</a> nor will the MongoDB server version be taken into account. Users are therefore encouraged to use specific read and/or write command methods if possible. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> The <em>"readPreference"</em> option does not control the server to which the driver issues the operation; it will always be executed on this server object. Instead, it may be used when issuing the operation to a secondary (from a replica set connection, not standalone) or mongos node to ensure that the driver sets the wire protocol accordingly or adds the read preference to the operation, respectively. </span> </p></blockquote> </div> <div class="refsect1 parameters" id="refsect1-mongodb-driver-server.executecommand-parameters"> <h3 class="title">Parameters</h3> <dl> <dt> <code class="parameter">db</code> (<span class="type"><a href="language.types.string.html" class="type string">string</a></span>)</dt> <dd> <p class="para"> The name of the database on which to execute the command. </p> </dd> <dt> <code class="parameter">command</code> (<a href="class.mongodb-driver-command.html" class="classname">MongoDB\Driver\Command</a>)</dt> <dd> <p class="para"> The command to execute. </p> </dd> <dt> <code class="parameter">options</code></dt> <dd> <p class="para"> <table class="doctable table"> <caption><strong>options</strong></caption> <thead> <tr> <th>Option</th> <th>Type</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>readConcern</td> <td><a href="class.mongodb-driver-readconcern.html" class="classname">MongoDB\Driver\ReadConcern</a></td> <td> <p class="para"> A read concern to apply to the operation. </p> <p class="para"> This option is available in MongoDB 3.2+ and will result in an exception at execution time if specified for an older server version. </p> </td> </tr> <tr> <td>readPreference</td> <td><a href="class.mongodb-driver-readpreference.html" class="classname">MongoDB\Driver\ReadPreference</a></td> <td> <p class="para"> A read preference to use for selecting a server for the operation. </p> </td> </tr> <tr> <td>session</td> <td><a href="class.mongodb-driver-session.html" class="classname">MongoDB\Driver\Session</a></td> <td> <p class="para"> A session to associate with the operation. </p> </td> </tr> <tr> <td>writeConcern</td> <td><a href="class.mongodb-driver-writeconcern.html" class="classname">MongoDB\Driver\WriteConcern</a></td> <td> <p class="para"> A write concern to apply to the operation. </p> </td> </tr> </tbody> </table> </p> <div class="warning"><strong class="warning">Warning</strong> <p class="para"> If you are using a <em>"session"</em> which has a transaction in progress, you cannot specify a <em>"readConcern"</em> or <em>"writeConcern"</em> option. This will result in an <a href="class.mongodb-driver-exception-invalidargumentexception.html" class="classname">MongoDB\Driver\Exception\InvalidArgumentException</a> being thrown. Instead, you should set these two options when you create the transaction with <span class="methodname"><a href="mongodb-driver-session.starttransaction.html" class="methodname">MongoDB\Driver\Session::startTransaction()</a></span>. </p> </div> </dd> </dl> </div> <div class="refsect1 returnvalues" id="refsect1-mongodb-driver-server.executecommand-returnvalues"> <h3 class="title">Return Values</h3> <p class="para">Returns <a href="class.mongodb-driver-cursor.html" class="classname">MongoDB\Driver\Cursor</a> on success.</p> </div> <div class="refsect1 errors" id="refsect1-mongodb-driver-server.executecommand-errors"> <h3 class="title">Errors/Exceptions</h3> <ul class="simplelist"> <li class="member">Throws <a href="class.mongodb-driver-exception-invalidargumentexception.html" class="classname">MongoDB\Driver\Exception\InvalidArgumentException</a> if the <em>"session"</em> option is used with an associated transaction in combination with a <em>"readConcern"</em> or <em>"writeConcern"</em> option.</li> <li class="member">Throws <a href="class.mongodb-driver-exception-invalidargumentexception.html" class="classname">MongoDB\Driver\Exception\InvalidArgumentException</a> if the <em>"session"</em> option is used in combination with an unacknowledged write concern.</li> <li class="member">Throws <a href="class.mongodb-driver-exception-invalidargumentexception.html" class="classname">MongoDB\Driver\Exception\InvalidArgumentException</a> on argument parsing errors.</li><li class="member">Throws <a href="class.mongodb-driver-exception-connectionexception.html" class="classname">MongoDB\Driver\Exception\ConnectionException</a> if connection to the server fails (for reasons other than authentication).</li><li class="member">Throws <a href="class.mongodb-driver-exception-authenticationexception.html" class="classname">MongoDB\Driver\Exception\AuthenticationException</a> if authentication is needed and fails.</li> <li class="member">Throws <a href="class.mongodb-driver-exception-runtimeexception.html" class="classname">MongoDB\Driver\Exception\RuntimeException</a> on other errors (e.g. invalid command, issuing a write command to a secondary).</li> </ul> </div> <div class="refsect1 changelog" id="refsect1-mongodb-driver-server.executecommand-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>1.4.4</td> <td> <a href="class.mongodb-driver-exception-invalidargumentexception.html" class="classname">MongoDB\Driver\Exception\InvalidArgumentException</a> will be thrown if the <em>"session"</em> option is used in combination with an unacknowledged write concern. </td> </tr> <tr> <td>1.4.0</td> <td> The third parameter is now an <code class="parameter">options</code> array. For backwards compatibility, this paramater will still accept a <a href="class.mongodb-driver-readpreference.html" class="classname">MongoDB\Driver\ReadPreference</a> object. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 notes" id="refsect1-mongodb-driver-server.executecommand-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> It is the caller's responsibility to ensure that the server is capable of executing the write operation. For example, executing a write operation on a secondary (excluding its "local" database) will fail. </span> </p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-mongodb-driver-server.executecommand-seealso"> <h3 class="title">See Also</h3> <ul class="simplelist"> <li class="member"><a href="class.mongodb-driver-command.html" class="classname">MongoDB\Driver\Command</a></li> <li class="member"><a href="class.mongodb-driver-cursor.html" class="classname">MongoDB\Driver\Cursor</a></li> <li class="member"><span class="function"><a href="mongodb-driver-server.executereadcommand.html" class="function" rel="rdfs-seeAlso">MongoDB\Driver\Server::executeReadCommand()</a> - Execute a database command that reads on this server</span></li> <li class="member"><span class="function"><a href="mongodb-driver-server.executereadwritecommand.html" class="function" rel="rdfs-seeAlso">MongoDB\Driver\Server::executeReadWriteCommand()</a> - Execute a database command that reads and writes on this server</span></li> <li class="member"><span class="function"><a href="mongodb-driver-server.executewritecommand.html" class="function" rel="rdfs-seeAlso">MongoDB\Driver\Server::executeWriteCommand()</a> - Execute a database command that writes on this server</span></li> <li class="member"><span class="function"><a href="mongodb-driver-manager.executecommand.html" class="function" rel="rdfs-seeAlso">MongoDB\Driver\Manager::executeCommand()</a> - Execute a database command</span></li> </ul> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongodb-driver-server.executebulkwrite.html">MongoDB\Driver\Server::executeBulkWrite</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongodb-driver-server.executequery.html">MongoDB\Driver\Server::executeQuery</a></div> <div class="up"><a href="class.mongodb-driver-server.html">MongoDB\Driver\Server</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>