-
Notifications
You must be signed in to change notification settings - Fork 1.5k
DOCSP-35980: Insert One usage example #2826
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
ccho-mongodb
merged 9 commits into
mongodb:4.1
from
norareidy:DOCSP-35980-insert-one-usage
Apr 18, 2024
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a78ca45
DOCSP-35980: Insert One usage example
norareidy 327c046
code edit
norareidy c45c82e
wording fixes
norareidy 8167050
RR feedback
norareidy ac9f022
merge
norareidy 1767824
assertion
norareidy d124f60
removing assertion
norareidy 001abc5
test
norareidy 54280ef
PRR rst and content fixes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Movie; | ||
use MongoDB\Laravel\Tests\TestCase; | ||
|
||
class InsertOneTest extends TestCase | ||
{ | ||
/** | ||
* @runInSeparateProcess | ||
* @preserveGlobalState disabled | ||
*/ | ||
public function testInsertOne(): void | ||
{ | ||
require_once __DIR__ . '/Movie.php'; | ||
|
||
Movie::truncate(); | ||
|
||
// begin-insert-one | ||
$movie = Movie::create([ | ||
'title' => 'Marriage Story', | ||
'year' => 2019, | ||
'runtime' => 136, | ||
]); | ||
|
||
echo $movie->toJson(); | ||
// end-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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
.. _laravel-insert-one-usage: | ||
|
||
================= | ||
Insert a Document | ||
================= | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: insert one, add one, code example | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 1 | ||
:class: singlecol | ||
|
||
You can insert a document into a collection by calling the ``create()`` method on | ||
an Eloquent model or query builder. | ||
|
||
To insert a document, call the ``create()`` method and specify the document's field values | ||
inside the method call. | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 | ||
|
||
.. input:: ../includes/usage-examples/InsertOneTest.php | ||
:start-after: begin-insert-one | ||
:end-before: end-insert-one | ||
:language: php | ||
:dedent: | ||
|
||
.. output:: | ||
:language: console | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
:visible: false | ||
|
||
{ | ||
"title": "Marriage Story", | ||
"year": 2019, | ||
"runtime": 136, | ||
"updated_at": "...", | ||
"created_at": "...", | ||
"_id": "..." | ||
} | ||
|
||
To learn how to edit your Laravel application to run the usage example, see the | ||
:ref:`Usage Examples landing page <laravel-usage-examples>`. | ||
|
||
.. 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q: will this be merged only after write operations is merged? Link is broken as of now There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I'll wait until after write operations is merged |
||
of the Write Operations guide. | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This example could be completed by the more expressive form (if anyone need to update the
$movie
model before saving):There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@GromNaN Would you recommend replacing the
create()
example with one that showssave()
? Ifsave()
seems more common and helpful, we could update the example to show that method of inserting a single document instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No,
Movie::create([...])
is the most common way to create a model.There is also
Movie::createOrFirst()
that can be used (usingfindOneAndUpdate
with upsert)