Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8355c30

Browse files
Merge 4.2 into 4.3 (#2885)
2 parents 8bc235c + d40b935 commit 8355c30

File tree

11 files changed

+322
-4
lines changed

11 files changed

+322
-4
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http\Controllers;
6+
7+
use App\Models\Movie;
8+
use MongoDB\Laravel\Tests\TestCase;
9+
10+
class InsertOneTest extends TestCase
11+
{
12+
/**
13+
* @runInSeparateProcess
14+
* @preserveGlobalState disabled
15+
*/
16+
public function testInsertOne(): void
17+
{
18+
require_once __DIR__ . '/Movie.php';
19+
20+
Movie::truncate();
21+
22+
// begin-insert-one
23+
$movie = Movie::create([
24+
'title' => 'Marriage Story',
25+
'year' => 2019,
26+
'runtime' => 136,
27+
]);
28+
29+
echo $movie->toJson();
30+
// end-insert-one
31+
32+
$this->assertInstanceOf(Movie::class, $movie);
33+
$this->expectOutputRegex('/^{"title":"Marriage Story","year":2019,"runtime":136,"updated_at":".{27}","created_at":".{27}","_id":"[a-z0-9]{24}"}$/');
34+
}
35+
}
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+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http\Controllers;
6+
7+
use App\Models\Movie;
8+
use MongoDB\Laravel\Tests\TestCase;
9+
10+
class UpdateManyTest extends TestCase
11+
{
12+
/**
13+
* @runInSeparateProcess
14+
* @preserveGlobalState disabled
15+
*/
16+
public function testUpdateMany(): void
17+
{
18+
require_once __DIR__ . '/Movie.php';
19+
20+
Movie::truncate();
21+
Movie::insert([
22+
[
23+
'title' => 'Hollywood',
24+
'imdb' => [
25+
'rating' => 9.1,
26+
'votes' => 511,
27+
],
28+
],
29+
[
30+
'title' => 'The Shawshank Redemption',
31+
'imdb' => [
32+
'rating' => 9.3,
33+
'votes' => 1513145,
34+
],
35+
],
36+
]);
37+
38+
// begin-update-many
39+
$updates = Movie::where('imdb.rating', '>', 9.0)
40+
->update(['acclaimed' => true]);
41+
42+
echo 'Updated documents: ' . $updates;
43+
// end-update-many
44+
45+
$this->assertEquals(2, $updates);
46+
$this->expectOutputString('Updated documents: 2');
47+
}
48+
}

‎docs/index.txt‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Fundamentals
6161
To learn how to perform the following tasks by using {+odm-short+},
6262
see the following content:
6363

64+
- :ref:`Manage Databases and Collections <laravel-db-coll>`
6465
- :ref:`laravel-fundamentals-read-ops`
6566
- :ref:`laravel-fundamentals-write-ops`
6667
- :ref:`laravel-eloquent-models`
@@ -76,6 +77,12 @@ Issues & Help
7677
Learn how to report bugs, contribute to the library, and find
7778
more resources in the :ref:`laravel-issues-and-help` section.
7879

80+
Feature Compatibility
81+
---------------------
82+
83+
Learn about Laravel features that {+odm-short+} supports in the
84+
:ref:`laravel-feature-compat` section.
85+
7986
Compatibility
8087
-------------
8188

‎docs/usage-examples.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,12 @@ calls the controller function and returns the result to a web interface.
7272
:maxdepth: 1
7373

7474
/usage-examples/findOne
75+
/usage-examples/insertOne
7576
/usage-examples/insertMany
7677
/usage-examples/updateOne
78+
/usage-examples/updateMany
7779
/usage-examples/deleteOne
7880
/usage-examples/deleteMany
7981
/usage-examples/count
8082
/usage-examples/distinct
83+
/usage-examples/runCommand

‎docs/usage-examples/deleteOne.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ This usage example performs the following actions:
3232
- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the
3333
``sample_mflix`` database
3434
- Deletes a document from the ``movies`` collection that matches a query filter
35+
- Prints the number of deleted documents
3536

3637
The example calls the following methods on the ``Movie`` model:
3738

‎docs/usage-examples/findOne.txt‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ Example
1919
This usage example performs the following actions:
2020

2121
- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the
22-
``sample_mflix`` database.
23-
- Retrieves a document from the ``movies`` collection that matches a query filter.
22+
``sample_mflix`` database
23+
- Retrieves a document from the ``movies`` collection that matches a query filter
24+
- Prints the retrieved document
2425

2526
The example calls the following methods on the ``Movie`` model:
2627

‎docs/usage-examples/insertOne.txt‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
.. _laravel-insert-one-usage:
2+
3+
=================
4+
Insert a Document
5+
=================
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: insert one, add one, code example
13+
14+
.. contents:: On this page
15+
:local:
16+
:backlinks: none
17+
:depth: 1
18+
:class: singlecol
19+
20+
You can insert a document into a collection by calling the ``create()`` method on
21+
an Eloquent model or query builder.
22+
23+
To insert a document, pass the data you need to insert as a document containing
24+
the fields and values to the ``create()`` method.
25+
26+
Example
27+
-------
28+
29+
This usage example performs the following actions:
30+
31+
- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the
32+
``sample_mflix`` database
33+
- Inserts a document into the ``movies`` collection
34+
35+
The example calls the ``create()`` method to insert a document that contains the following
36+
information:
37+
38+
- ``title`` value of ``"Marriage Story"``
39+
- ``year`` value of ``2019``
40+
- ``runtime`` value of ``136``
41+
42+
.. io-code-block::
43+
:copyable: true
44+
45+
.. input:: ../includes/usage-examples/InsertOneTest.php
46+
:start-after: begin-insert-one
47+
:end-before: end-insert-one
48+
:language: php
49+
:dedent:
50+
51+
.. output::
52+
:language: json
53+
:visible: false
54+
55+
{
56+
"title": "Marriage Story",
57+
"year": 2019,
58+
"runtime": 136,
59+
"updated_at": "...",
60+
"created_at": "...",
61+
"_id": "..."
62+
}
63+
64+
To learn how to edit your Laravel application to run the usage example, see the
65+
:ref:`Usage Examples landing page <laravel-usage-examples>`.
66+
67+
.. tip::
68+
69+
You can also use the ``save()`` or ``insert()`` methods to insert a document into a collection.
70+
To learn more about insert operations, see the :ref:`laravel-fundamentals-insert-documents` section
71+
of the Write Operations guide.
72+
73+

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

‎docs/usage-examples/updateMany.txt‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
.. _laravel-update-one-usage:
2+
3+
=========================
4+
Update Multiple Documents
5+
=========================
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: update many, modify, code example
13+
14+
.. contents:: On this page
15+
:local:
16+
:backlinks: none
17+
:depth: 1
18+
:class: singlecol
19+
20+
You can update multiple documents in a collection by calling the ``update()`` method
21+
on a query builder.
22+
23+
Pass a query filter to the ``where()`` method to retrieve documents that meet a
24+
set of criteria. Then, update the matching documents by passing your intended
25+
document changes to the ``update()`` method.
26+
27+
Example
28+
-------
29+
30+
This usage example performs the following actions:
31+
32+
- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the
33+
``sample_mflix`` database
34+
- Updates documents from the ``movies`` collection that match a query filter
35+
- Prints the number of updated documents
36+
37+
The example calls the following methods on the ``Movie`` model:
38+
39+
- ``where()``: matches documents in which the value of the ``imdb.rating`` nested field
40+
is greater than ``9``.
41+
- ``update()``: updates the matching documents by adding an ``acclaimed`` field and setting
42+
its value to ``true``. This method returns the number of documents that were successfully
43+
updated.
44+
45+
.. io-code-block::
46+
:copyable: true
47+
48+
.. input:: ../includes/usage-examples/UpdateManyTest.php
49+
:start-after: begin-update-many
50+
:end-before: end-update-many
51+
:language: php
52+
:dedent:
53+
54+
.. output::
55+
:language: console
56+
:visible: false
57+
58+
Updated documents: 20
59+
60+
To learn how to edit your Laravel application to run the usage example, see the
61+
:ref:`Usage Examples landing page <laravel-usage-examples>`.
62+
63+
.. tip::
64+
65+
To learn more about updating data with {+odm-short+}, see the :ref:`laravel-fundamentals-modify-documents`
66+
section of the Write Operations guide.
67+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /