Skip to content

Commit 839b411

Browse files
authored
DOCSP-35983: Run command usage example (#2852)
* DOCSP-35983: Run command usage example
1 parent 26d9632 commit 839b411

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http\Controllers;
6+
7+
use Illuminate\Support\Facades\DB;
8+
use MongoDB\Driver\Cursor;
9+
use MongoDB\Laravel\Tests\TestCase;
10+
11+
class RunCommandTest extends TestCase
12+
{
13+
public function testRunCommand(): void
14+
{
15+
// begin-command
16+
$cursor = DB::connection('mongodb')
17+
->command(['listCollections' => 1]);
18+
19+
foreach ($cursor as $coll) {
20+
echo $coll['name'] . "<br>\n";
21+
}
22+
23+
// end-command
24+
25+
$this->assertNotNull($cursor);
26+
$this->assertInstanceOf(Cursor::class, $cursor);
27+
$this->expectOutputRegex('/<br>/');
28+
}
29+
}

docs/usage-examples.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,4 @@ calls the controller function and returns the result to a web interface.
8080
/usage-examples/deleteMany
8181
/usage-examples/count
8282
/usage-examples/distinct
83+
/usage-examples/runCommand

docs/usage-examples/runCommand.txt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
.. _laravel-run-command-usage:
2+
3+
=============
4+
Run a Command
5+
=============
6+
7+
You can run a MongoDB command directly on a database by calling the ``command()``
8+
method on a database connection instance.
9+
10+
To run a command, call the ``command()`` method and pass it a document that
11+
contains the command and its parameters.
12+
13+
Example
14+
-------
15+
16+
This usage example performs the following actions on the database connection
17+
instance that uses the ``sample_mflix`` database:
18+
19+
- Creates a database connection instance that references the ``sample_mflix``
20+
database
21+
- Specifies a command to retrieve a list of collections and views in the
22+
``sample_mflix`` database
23+
- Prints the value of the ``name`` field of each result returned by the command
24+
25+
The example calls the ``command()`` method to run the ``listCollections`` command. This method
26+
returns a cursor that contains a result document for each collection in the database.
27+
28+
.. io-code-block::
29+
30+
.. input:: ../includes/usage-examples/RunCommandTest.php
31+
:start-after: begin-command
32+
:end-before: end-command
33+
:language: php
34+
:dedent:
35+
36+
.. output::
37+
:language: console
38+
:visible: false
39+
40+
sessions
41+
movies
42+
theaters
43+
comments
44+
embedded_movies
45+
users
46+
47+
To learn how to edit your Laravel application to run the usage example, see the
48+
:ref:`Usage Examples landing page <laravel-usage-examples>`.
49+
50+
.. tip::
51+
52+
To learn more about running MongoDB database commands, see
53+
:manual:`Database Commands </reference/command/>` in the {+server-docs-name+}.

0 commit comments

Comments
 (0)