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 5 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
31 changes: 31 additions & 0 deletions docs/includes/usage-examples/RunCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use MongoDB\Driver\Cursor;
use MongoDB\Laravel\Tests\TestCase;

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

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

// end-command

$this->assertNotNull($cursor);
$this->assertInstanceOf(Cursor::class, $cursor);
}
}
1 change: 1 addition & 0 deletions docs/usage-examples.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,4 @@ calls the controller function and returns the result to a web interface.
/usage-examples/findOne
/usage-examples/updateOne
/usage-examples/deleteOne
/usage-examples/runCommand
52 changes: 52 additions & 0 deletions docs/usage-examples/runCommand.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
.. _laravel-run-command-usage:

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

You can run a command directly on a database by calling the ``command()``
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:

I think it could be helpful to specify that it's a MongoDB command to avoid confusion, similar to how it's used in the admonition at the end of the page.

Suggested change
You can run a command directly on a database by calling the ``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 specify the database command
document inside the method call. This document includes the command name and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:

I think "database command document" might not be a common term, so avoiding it could also avoid having to provide the explanation in the next sentence.

Suggested change
To run a command, call the ``command()`` method and specify the database command
document inside the method call. This document includes the command name and
To run a command, call the ``command()`` method and pass it a database command.

value.

Example
-------

This usage example performs the following actions:

- Creates a database connection instance to access the ``sample_mflix`` database
- Specifies a command to retrieve a list of collections in the ``sample_mflix`` database
- Prints the name of each ``sample_mflix`` collection
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue:

There's no reference to "sample_mflix" in the code example.

Suggestion:

I think it could be good to either describe as the database configured for your "mongodb" connection or to add some information before this list that mentions that the mongodb connection is set to sample_mflix.


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+}.