Skip to content

DOCSP-35983: Run command usage example #2852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions docs/includes/usage-examples/RunCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use MongoDB\Driver\Cursor;
use MongoDB\Laravel\Tests\TestCase;

class RunCommandTest extends TestCase
{
public function testRunCommand(): void
{
// begin-command
$cursor = DB::connection('mongodb')
->command(['listCollections' => 1]);

foreach ($cursor as $coll) {
echo $coll['name'] . "<br>\n";
}

// end-command

$this->assertNotNull($cursor);
$this->assertInstanceOf(Cursor::class, $cursor);
$this->expectOutputRegex('/<br>/');
}
}
1 change: 1 addition & 0 deletions docs/usage-examples.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,4 @@ calls the controller function and returns the result to a web interface.
/usage-examples/deleteMany
/usage-examples/count
/usage-examples/distinct
/usage-examples/runCommand
53 changes: 53 additions & 0 deletions docs/usage-examples/runCommand.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
.. _laravel-run-command-usage:

=============
Run a Command
=============

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``
database
- Specifies a command to retrieve a list of collections and views in the
``sample_mflix`` database
- Prints 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.

.. io-code-block::

.. input:: ../includes/usage-examples/RunCommandTest.php
:start-after: begin-command
:end-before: end-command
:language: php
:dedent:

.. output::
:language: console
:visible: false

sessions
movies
theaters
comments
embedded_movies
users

To learn how to edit your Laravel application to run the usage example, see the
:ref:`Usage Examples landing page <laravel-usage-examples>`.

.. tip::

To learn more about running MongoDB database commands, see
:manual:`Database Commands </reference/command/>` in the {+server-docs-name+}.