Run a Command
On this page
You can run a MongoDB command directly on a database by calling the command()
method on a database connection instance.
To run a command, call the command()
method and pass it a document that
contains the command and its parameters.
Example
This usage example performs the following actions on the database connection
instance that uses the sample_mflix
database:
Creates a database connection instance that references the
sample_mflix
databaseSpecifies a command to retrieve a list of collections and views in the
sample_mflix
databasePrints the value of the
name
field of each result returned by the command
The example calls the command()
method to run the listCollections
command. This method
returns a cursor that contains a result document for each collection in the database.
$cursor = DB::connection('mongodb') ->command(['listCollections' => 1]); foreach ($cursor as $coll) { echo $coll['name'] . "<br>\n"; }
sessions movies theaters comments embedded_movies users
To learn how to edit your Laravel application to run the usage example, see the Usage Examples landing page.
Tip
To learn more about running MongoDB database commands, see Database Commands in the Server manual.