Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rustagir committed Jan 30, 2025
1 parent 7cb0b12 commit f498f97
Show file tree
Hide file tree
Showing 7 changed files with 288 additions and 123 deletions.
4 changes: 2 additions & 2 deletions docs/includes/usage-examples/FindOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ public function testFindOne(): void
// end-eloquent-find-one

$this->assertInstanceOf(Movie::class, $movie);
$this->expectOutputRegex('/^{"_id":"[a-z0-9]{24}","title":"The Shawshank Redemption","directors":\["Frank Darabont","Rob Reiner"\]}$/');
$this->assertSame($movie->title, 'The Shawshank Redemption');

// begin-qb-find-one
$movie = DB::table('movies')
->where('directors', 'Rob Reiner')
->orderBy('_id')
->first();

echo $movie[0]->toJson();
echo print_r($movie);
// end-qb-find-one
}
}
18 changes: 15 additions & 3 deletions docs/includes/usage-examples/InsertOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Http\Controllers;

use App\Models\Movie;
use Illuminate\Support\Facades\DB;
use MongoDB\Laravel\Tests\TestCase;

class InsertOneTest extends TestCase
Expand All @@ -19,17 +20,28 @@ public function testInsertOne(): void

Movie::truncate();

// begin-insert-one
// begin-eloquent-insert-one
$movie = Movie::create([
'title' => 'Marriage Story',
'year' => 2019,
'runtime' => 136,
]);

echo $movie->toJson();
// end-insert-one
// end-eloquent-insert-one

// begin-qb-insert-one
$success = DB::table('movies')
->insert([
'title' => 'Marriage Story',
'year' => 2019,
'runtime' => 136,
]);

echo 'Insert operation success: ' . ($success ? 'yes' : 'no');
// end-qb-insert-one

$this->assertInstanceOf(Movie::class, $movie);
$this->expectOutputRegex('/^{"title":"Marriage Story","year":2019,"runtime":136,"updated_at":".{27}","created_at":".{27}","_id":"[a-z0-9]{24}"}$/');
$this->assertSame($movie->title, 'Marriage Story');
}
}
16 changes: 13 additions & 3 deletions docs/includes/usage-examples/UpdateManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Http\Controllers;

use App\Models\Movie;
use Illuminate\Support\Facades\DB;
use MongoDB\Laravel\Tests\TestCase;

class UpdateManyTest extends TestCase
Expand Down Expand Up @@ -35,14 +36,23 @@ public function testUpdateMany(): void
],
]);

// begin-update-many
// begin-eloquent-update-many
$updates = Movie::where('imdb.rating', '>', 9.0)
->update(['acclaimed' => true]);

echo 'Updated documents: ' . $updates;
// end-update-many
// end-eloquent-update-many

$this->assertEquals(2, $updates);
$this->expectOutputString('Updated documents: 2');

// begin-qb-update-many
$updates = DB::table('movies')
->where('imdb.rating', '>', 9.0)
->update(['acclaimed' => true]);

echo 'Updated documents: ' . $updates;
// end-qb-update-many

$this->expectOutputString('Updated documents: 2Updated documents: 0');
}
}
23 changes: 20 additions & 3 deletions docs/includes/usage-examples/UpdateOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Http\Controllers;

use App\Models\Movie;
use Illuminate\Support\Facades\DB;
use MongoDB\Laravel\Tests\TestCase;

class UpdateOneTest extends TestCase
Expand All @@ -28,7 +29,7 @@ public function testUpdateOne(): void
],
]);

// begin-update-one
// begin-eloquent-update-one
$updates = Movie::where('title', 'Carol')
->orderBy('_id')
->first()
Expand All @@ -40,9 +41,25 @@ public function testUpdateOne(): void
]);

echo 'Updated documents: ' . $updates;
// end-update-one
// end-eloquent-update-one

$this->assertTrue($updates);
$this->expectOutputString('Updated documents: 1');

// begin-qb-update-one
$updates = DB::table('movies')
->where('title', 'Carol')
->orderBy('_id')
->first()
->update([
'imdb' => [
'rating' => 7.3,
'votes' => 142000,
],
]);

echo 'Updated documents: ' . $updates;
// end-qb-update-one

$this->expectOutputString('Updated documents: 1Updated documents: 0');
}
}
122 changes: 80 additions & 42 deletions docs/usage-examples/insertOne.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,50 +23,88 @@ an Eloquent model or query builder.
To insert a document, pass the data you need to insert as a document containing
the fields and values to the ``create()`` method.

Example
-------

This usage example performs the following actions:

- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the
``sample_mflix`` database
- Inserts a document into the ``movies`` collection

The example calls the ``create()`` method to insert a document that contains the following
information:

- ``title`` value of ``"Marriage Story"``
- ``year`` value of ``2019``
- ``runtime`` value of ``136``

.. io-code-block::
:copyable: true
.. tip::

.. input:: ../includes/usage-examples/InsertOneTest.php
:start-after: begin-insert-one
:end-before: end-insert-one
:language: php
:dedent:
You can also use the ``save()`` or ``insert()`` methods to insert a
document into a collection. To learn more about insert operations,
see the :ref:`laravel-fundamentals-insert-documents` section of the
Write Operations guide.

.. output::
:language: json
:visible: false
Example
-------

{
"title": "Marriage Story",
"year": 2019,
"runtime": 136,
"updated_at": "...",
"created_at": "...",
"_id": "..."
}
Select from the following :guilabel:`Eloquent` and :guilabel:`Query
Builder` tabs to view usage examples for the same operation that use
each corresponding query syntax:

.. tabs::

.. tab:: Eloquent
:tabid: eloquent-model-count

This example performs the following actions:

- Uses the ``Movie`` Eloquent model to represent the ``movies``
collection in the ``sample_mflix`` database
- Inserts a document into the ``movies`` collection

The example calls the ``create()`` method to insert a document
that contains the following fields and values:

- ``title`` value of ``"Marriage Story"``
- ``year`` value of ``2019``
- ``runtime`` value of ``136``

.. io-code-block::
:copyable: true

.. input:: ../includes/usage-examples/InsertOneTest.php
:start-after: begin-eloquent-insert-one
:end-before: end-eloquent-insert-one
:language: php
:dedent:

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

{
"title": "Marriage Story",
"year": 2019,
"runtime": 136,
"updated_at": "...",
"created_at": "...",
"_id": "..."
}

.. tab:: Query Builder
:tabid: query-builder-count

This example performs the following actions:

- Accesses the ``movies`` collection by calling the ``table()``
method from the ``DB`` facade
- Inserts a document into the ``movies`` collection

The example calls the ``create()`` method to insert a document
that contains the following fields and values:

- ``title`` value of ``"Marriage Story"``
- ``year`` value of ``2019``
- ``runtime`` value of ``136``

.. io-code-block::

.. input:: ../includes/usage-examples/InsertOneTest.php
:start-after: begin-qb-insert-one
:end-before: end-qb-insert-one
:language: php
:dedent:

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

Insert operation success: yes

.. include:: /includes/usage-examples/fact-edit-laravel-app.rst

.. tip::

You can also use the ``save()`` or ``insert()`` methods to insert a document into a collection.
To learn more about insert operations, see the :ref:`laravel-fundamentals-insert-documents` section
of the Write Operations guide.


112 changes: 77 additions & 35 deletions docs/usage-examples/updateMany.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,43 +24,85 @@ Pass a query filter to the ``where()`` method to retrieve documents that meet a
set of criteria. Then, update the matching documents by passing your intended
document changes to the ``update()`` method.

Example
-------

This usage example performs the following actions:

- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the
``sample_mflix`` database
- Updates documents from the ``movies`` collection that match a query filter
- Prints the number of updated documents

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

- ``where()``: matches documents in which the value of the ``imdb.rating`` nested field
is greater than ``9.0``.
- ``update()``: updates the matching documents by adding an ``acclaimed`` field and setting
its value to ``true``. This method returns the number of documents that were successfully
updated.

.. io-code-block::
:copyable: true

.. input:: ../includes/usage-examples/UpdateManyTest.php
:start-after: begin-update-many
:end-before: end-update-many
:language: php
:dedent:

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

Updated documents: 20

.. include:: /includes/usage-examples/fact-edit-laravel-app.rst

.. tip::

To learn more about updating data with the {+odm-short+}, see the :ref:`laravel-fundamentals-modify-documents`
section of the Write Operations guide.

Example
-------

Select from the following :guilabel:`Eloquent` and :guilabel:`Query
Builder` tabs to view usage examples for the same operation that use
each corresponding query syntax:

.. tabs::

.. tab:: Eloquent
:tabid: eloquent-model-count

This example performs the following actions:

- Uses the ``Movie`` Eloquent model to represent the ``movies``
collection in the ``sample_mflix`` database
- Updates documents from the ``movies`` collection that match a
query filter
- Prints the number of updated documents

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

- ``where()``: Matches documents in which the value of the
``imdb.rating`` nested field is greater than ``9.0``
- ``update()``: Updates the matching documents by adding an
``acclaimed`` field and setting its value to ``true``, then
returns the number of updated documents

.. io-code-block::
:copyable: true

.. input:: ../includes/usage-examples/UpdateManyTest.php
:start-after: begin-eloquent-update-many
:end-before: end-eloquent-update-many
:language: php
:dedent:

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

Updated documents: 20

.. tab:: Query Builder
:tabid: query-builder-count

This example performs the following actions:

- Accesses the ``movies`` collection by calling the ``table()``
method from the ``DB`` facade
- Updates documents from the ``movies`` collection that match a
query filter
- Prints the number of updated documents

The example calls the following query builder methods:

- ``where()``: Matches documents in which the value of the
``imdb.rating`` nested field is greater than ``9.0``
- ``update()``: Updates the matching documents by adding an
``acclaimed`` field and setting its value to ``true``, then
returns the number of updated documents

.. io-code-block::

.. input:: ../includes/usage-examples/UpdateManyTest.php
:start-after: begin-qb-update-many
:end-before: end-qb-update-many
:language: php
:dedent:

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

Updated documents: 20

.. include:: /includes/usage-examples/fact-edit-laravel-app.rst
Loading

0 comments on commit f498f97

Please sign in to comment.