-
Notifications
You must be signed in to change notification settings - Fork 1.5k
DOCSP-35978: Insert multiple usage example #2849
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 11 commits into
mongodb:4.1
from
norareidy:DOCSP-35978-insert-multiple-usage
Apr 16, 2024
Merged
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
acad49a
DOCSP-35978: Insert multiple usage example
norareidy ca908d6
code test
norareidy b04b574
apply phpcbf formatting
norareidy d01673d
reword
norareidy 2166732
Merge branch 'DOCSP-35978-insert-multiple-usage' of github.com:norare…
norareidy e21c588
JT feedback
norareidy f97fa39
apply phpcbf formatting
norareidy 515f121
wording edits
norareidy 8d25931
MW feedback
norareidy 89e8cca
Add missing use classes
GromNaN b4583bb
Merge branch '4.1' into DOCSP-35978-insert-multiple-usage
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,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Movie; | ||
use MongoDB\Laravel\Tests\TestCase; | ||
|
||
class InsertManyTest extends TestCase | ||
{ | ||
/** | ||
* @runInSeparateProcess | ||
* @preserveGlobalState disabled | ||
*/ | ||
public function testInsertMany(): void | ||
{ | ||
require_once __DIR__ . '/Movie.php'; | ||
|
||
Movie::truncate(); | ||
|
||
// begin-insert-many | ||
$result = Movie::insert([ | ||
[ | ||
'title' => 'Anatomy of a Fall', | ||
'year' => 2023, | ||
], | ||
[ | ||
'title' => 'The Boy and the Heron', | ||
'year' => 2023, | ||
], | ||
[ | ||
'title' => 'Passages', | ||
'year' => 2023, | ||
], | ||
]); | ||
|
||
echo 'Insert operation success: ' . $result; | ||
// end-insert-many | ||
|
||
$this->assertTrue($result); | ||
$this->expectOutputString('Insert operation success: 1'); | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
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,62 @@ | ||
.. _laravel-insert-many-usage: | ||
|
||
========================= | ||
Insert Multiple Documents | ||
========================= | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: insert many, add, create, bulk, code example | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 1 | ||
:class: singlecol | ||
|
||
You can insert multiple documents into a collection by calling the ``insert()`` | ||
method on an Eloquent model or a query builder. | ||
|
||
To insert multiple documents, call the ``insert()`` method and specify the documents' field | ||
values as an array inside the method call. | ||
|
||
Example | ||
------- | ||
|
||
This usage example performs the following actions: | ||
|
||
- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the | ||
``sample_mflix`` database | ||
- Inserts documents into the ``movies`` collection | ||
- Prints the result of the insert operation | ||
|
||
The example calls the ``insert()`` method to insert documents in which the value of the | ||
``year`` field is ``2023``. This method returns a value of ``1`` if the operation is successful | ||
and throws an exception if the operation is unsuccessful. | ||
|
||
.. io-code-block:: | ||
:copyable: true | ||
|
||
.. input:: ../includes/usage-examples/InsertManyTest.php | ||
:start-after: begin-insert-many | ||
:end-before: end-insert-many | ||
:language: php | ||
:dedent: | ||
|
||
.. output:: | ||
:language: console | ||
:visible: false | ||
|
||
Insert operation success: 1 | ||
|
||
For instructions on editing your Laravel application to run the usage example, see the | ||
:ref:`Usage Example landing page <laravel-usage-examples>`. | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. tip:: | ||
|
||
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. Note: will merge after #2804
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.
You should add an example with a DateTime object, because
casts
are not applied whenModel::insert
is used. I think we've already discussed this with @ccho-mongodb in another PR, which I can't find.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.
Could you mention why we need to explicitly set the UTCDateTime? (because model casts are not applied).