Skip to content
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

Merge 4.1 into 4.2 #2818

Closed
wants to merge 3 commits into from
Closed
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
12 changes: 12 additions & 0 deletions docs/includes/usage-examples/Movie.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Models;

use MongoDB\Laravel\Eloquent\Model;

class Movie extends Model
{
protected $connection = 'mongodb';
protected $collection = 'movies';
protected $fillable = ['title', 'year', 'runtime', 'imdb', 'plot'];
}
48 changes: 48 additions & 0 deletions docs/includes/usage-examples/UpdateOneTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use App\Models\Movie;
use MongoDB\Laravel\Tests\TestCase;

class UpdateOneTest extends TestCase
{
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testUpdateOne(): void
{
require_once __DIR__ . '/Movie.php';

Movie::truncate();
Movie::insert([
[
'title' => 'Carol',
'imdb' => [
'rating' => 7.2,
'votes' => 125000,
],
],
]);

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

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

$this->assertTrue($updates);
$this->expectOutputString('Updated documents: 1');
}
}
9 changes: 8 additions & 1 deletion docs/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Laravel MongoDB
:maxdepth: 1

/quick-start
/usage-examples
Release Notes <https://github.com/mongodb/laravel-mongodb/releases/>
/retrieve
/eloquent-models
Expand Down Expand Up @@ -47,10 +48,16 @@ Learn how to add {+odm-short+} to a Laravel web application, connect to
MongoDB hosted on MongoDB Atlas, and begin working with data in the
:ref:`laravel-quick-start` section.

Usage Examples
--------------

See fully runnable code examples and explanations of common
MongoDB operations in the :ref:`laravel-usage-examples` section.

Fundamentals
------------

To learn how to perform the following tasks by using the {+odm-short+},
To learn how to perform the following tasks by using {+odm-short+},
see the following content:

- :ref:`laravel-fundamentals-retrieve`
Expand Down
2 changes: 1 addition & 1 deletion docs/quick-start/view-data.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.. laravel-quick-start-view-data:
.. _laravel-quick-start-view-data:

=================
View MongoDB Data
Expand Down
74 changes: 74 additions & 0 deletions docs/usage-examples.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
.. _laravel-usage-examples:

==============
Usage Examples
==============

.. facet::
:name: genre
:values: reference

.. meta::
:keywords: set up, runnable

.. contents:: On this page
:local:
:backlinks: none
:depth: 2
:class: singlecol

Overview
--------

Usage examples show runnable code examples to demonstrate frequently used MongoDB
operations. Each usage example includes the following components:

- Explanation of the MongoDB operation
- Example code that you can run from an application controller
- Output displayed by the print statement

How to Use the Usage Examples
-----------------------------

To learn how to add a usage example to your Laravel application and view the expected output,
see the following sections:

- :ref:`before-start`
- :ref:`run-usage-examples`

.. _before-start:

Before You Get Started
~~~~~~~~~~~~~~~~~~~~~~

You can run the usage examples from your own Laravel application or from the
``{+quickstart-app-name+}`` application created in the :ref:`laravel-quick-start` guide.

The usage examples are designed to run operations on a MongoDB deployment that contains
the MongoDB Atlas sample datasets. Before running the usage examples, ensure that you load
the sample data into the MongoDB cluster to which your application connects. Otherwise, the
operation output might not match the text included in the ``{+code-output-label+}`` tab of
the usage example page.

.. tip::

For instructions on loading the sample data into a MongoDB cluster, see
:atlas:`Load Sample Data </sample-data>` in the Atlas documentation.

.. _run-usage-examples:

Run the Usage Example
~~~~~~~~~~~~~~~~~~~~~

Each usage example page includes sample code that demonstrates a MongoDB operation and prints
a result. To run the operation, you can copy the sample code to a controller endpoint in your
Laravel application.

To view the expected output of the operation, you can add a web route to your application that
calls the controller function and returns the result to a web interface.

.. toctree::
:titlesonly:
:maxdepth: 1

/usage-examples/updateOne
67 changes: 67 additions & 0 deletions docs/usage-examples/updateOne.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
.. _laravel-update-one-usage:

=================
Update a Document
=================

.. facet::
:name: genre
:values: reference

.. meta::
:keywords: update one, modify, code example

.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol

You can update a document in a collection by retrieving a single document and calling
the ``update()`` method on an Eloquent model or a query builder.

Pass a query filter to the ``where()`` method, sort the matching documents, and call the
``first()`` method to retrieve only the first document. Then, update this matching document
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 a document from the ``movies`` collection that matches a query filter.

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

- ``where()``: matches documents in which the value of the ``title`` field is ``'Carol'``.
- ``orderBy()``: sorts matched documents by their ascending ``_id`` values.
- ``first()``: retrieves only the first matching document.
- ``update()``: updates the value of the ``imdb.rating`` nested field to from ``6.9`` to
``7.3``. This method also updates the ``imdb.votes`` nested field from ``493`` to ``142000``.

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

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

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

Updated documents: 1

For instructions on editing your Laravel application to run the usage example, see the
:ref:`Usage Example landing page <laravel-usage-examples>`.

.. tip::

To learn more about updating data with {+odm-short+}, see the `Updates
<https://laravel.com/docs/{+laravel-docs-version+}/eloquent#updates>`__ section of the
Laravel documentation.

3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<testsuite name="Test Suite">
<directory>tests/</directory>
</testsuite>
<testsuite name="Documentation">
<directory>docs/includes/</directory>
</testsuite>
</testsuites>
<php>
<env name="MONGODB_URI" value="mongodb://mongodb/"/>
Expand Down
Loading