-
Notifications
You must be signed in to change notification settings - Fork 34
DOCSP-41967: Insert documents #116
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
norareidy
merged 8 commits into
mongodb:php-standardization
from
norareidy:DOCSP-41967-insert
Sep 9, 2024
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
70f1b80
DOCSP-41967: Insert documents
norareidy ff1a86d
build
norareidy 745d5fe
snooty
norareidy 404bf92
edit
norareidy 3add653
JS feedback
norareidy 6b5d620
JT feedback
norareidy 519dc41
JT feedback 2
norareidy 3ea080a
Merge remote-tracking branch 'upstream/php-standardization' into DOCS…
norareidy 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
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,37 @@ | ||
<?php | ||
|
||
require 'vendor/autoload.php'; | ||
|
||
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset'); | ||
$client = new MongoDB\Client($uri); | ||
|
||
// start-db-coll | ||
$collection = $client->sample_restaurants->restaurants; | ||
// end-db-coll | ||
|
||
// Inserts a document that stores a "name" value of "Mongo's Burgers" into the collection | ||
// start-insert-one | ||
$result = $collection->insertOne(['name' => 'Mongo\'s Burgers']); | ||
// end-insert-one | ||
|
||
// Inserts documents representing restaurants into the collection | ||
// start-insert-many | ||
$restaurants = [ | ||
['name' => 'Mongo\'s Burgers'], | ||
['name' => 'Mongo\'s Pizza'] | ||
]; | ||
|
||
$result = $collection->insertMany($restaurants); | ||
// end-insert-many | ||
|
||
// Inserts multiple documents and instructs the insert operation to skip document-level validation | ||
// start-modify | ||
$docs = [ | ||
['name' => 'Mongo\'s Burgers'], | ||
['name' => 'Mongo\'s Pizza'], | ||
['name' => 'Mongo\'s Tacos'] | ||
]; | ||
|
||
$result = $collection->insertMany($docs, ['bypassDocumentValidation' => true]); | ||
// end-modify | ||
|
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 |
---|---|---|
|
@@ -12,6 +12,7 @@ MongoDB PHP Library | |
|
||
Get Started </get-started> | ||
/read | ||
/write | ||
/tutorial | ||
/upgrade | ||
/reference | ||
|
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,11 @@ | ||
.. _php-write: | ||
|
||
===================== | ||
Write Data to MongoDB | ||
===================== | ||
|
||
.. toctree:: | ||
:titlesonly: | ||
:maxdepth: 1 | ||
|
||
/write/insert |
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,171 @@ | ||
.. _php-write-insert: | ||
|
||
================ | ||
Insert Documents | ||
================ | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: code example, write, save, create | ||
|
||
Overview | ||
-------- | ||
|
||
In this guide, you can learn how to use the {+php-library+} to add | ||
documents to a MongoDB collection by performing **insert operations**. | ||
|
||
An insert operation inserts one or more documents into a MongoDB collection. | ||
You can perform an insert operation by using the ``MongoDB\Collection::insertOne()`` | ||
method to insert a single document or the ``MongoDB\Collection::insertMany()`` | ||
method to insert one or more documents. | ||
|
||
Sample Data | ||
~~~~~~~~~~~ | ||
|
||
The examples in this guide use the ``restaurants`` collection in the ``sample_restaurants`` | ||
database from the :atlas:`Atlas sample datasets </sample-data>`. To access this collection | ||
from your PHP application, instantiate a ``MongoDB\Client`` that connects to an Atlas cluster | ||
and assign the following value to your ``collection`` variable: | ||
|
||
.. literalinclude:: /includes/write/insert.php | ||
:language: php | ||
:dedent: | ||
:start-after: start-db-coll | ||
:end-before: end-db-coll | ||
|
||
To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the | ||
:atlas:`Get Started with Atlas </getting-started>` guide. | ||
|
||
The ``_id`` Field | ||
----------------- | ||
|
||
In a MongoDB collection, each document *must* contain an ``_id`` field | ||
with a unique field value. | ||
|
||
MongoDB allows you to manage this field in two ways: | ||
|
||
- You can set this field for each document yourself, ensuring each | ||
``_id`` field value is unique. | ||
- You can let the driver automatically generate unique ``ObjectId`` | ||
values for each document ``_id``. | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Unless you can guarantee uniqueness, we recommend | ||
letting the driver automatically generate ``_id`` values. | ||
|
||
.. note:: | ||
|
||
Duplicate ``_id`` values violate unique index constraints, which | ||
causes the driver to return a ``MongoDB\Driver\Exception\BulkWriteException`` | ||
error. | ||
|
||
To learn more about the ``_id`` field, see the | ||
:manual:`Unique Indexes </core/index-unique/>` guide in the {+mdb-server+} manual. | ||
|
||
To learn more about document structure and rules, see the | ||
:manual:`Documents </core/document>` guide in the {+mdb-server+} manual. | ||
|
||
Insert One Document | ||
------------------- | ||
|
||
To add a single document to a MongoDB collection, call the ``MongoDB\Collection::insertOne()`` | ||
method and pass the document you want to add. | ||
|
||
The following example inserts a document into the ``restaurants`` collection: | ||
|
||
.. literalinclude:: /includes/write/insert.php | ||
:language: php | ||
:dedent: | ||
:start-after: start-insert-one | ||
:end-before: end-insert-one | ||
|
||
Insert Multiple Documents | ||
------------------------- | ||
|
||
To add multiple documents to a MongoDB collection, call the ``MongoDB\Collection::insertMany()`` | ||
method and pass an array that stores the documents you want to add. | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The following example inserts two documents into the ``restaurants`` collection: | ||
|
||
.. literalinclude:: /includes/write/insert.php | ||
:language: php | ||
:dedent: | ||
:start-after: start-insert-many | ||
:end-before: end-insert-many | ||
|
||
Modify Insert Behavior | ||
---------------------- | ||
|
||
You can modify the behavior of the ``MongoDB\Collection::insertOne()`` and | ||
``MongoDB\Collection::insertMany()`` methods by passing an array that specifies | ||
option values as a parameter. The following table describes some options | ||
you can set in the array: | ||
|
||
.. list-table:: | ||
:widths: 30 70 | ||
:header-rows: 1 | ||
|
||
* - Field | ||
- Description | ||
|
||
* - ``bypassDocumentValidation`` | ||
- | If set to ``true``, allows the write to opt out of | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
:manual:`document-level validation </core/schema-validation>`. | ||
| Defaults to ``false``. | ||
| **Type**: ``bool`` | ||
|
||
* - ``writeConcern`` | ||
- | Sets the write concern for the operation. | ||
| Defaults to the write concern of the namespace. | ||
| **Type**: ``MongoDB\Driver\WriteConcern`` | ||
|
||
* - ``ordered`` | ||
- | If set to ``true``, the operation stops inserting documents when one insert | ||
fails. If ``false``, the operation continues to insert the remaining documents | ||
when one insert fails. You cannot pass this option to the ``insertOne()`` method. | ||
| Defaults to ``true``. | ||
| **Type**: ``bool`` | ||
|
||
* - ``comment`` | ||
- | A comment to attach to the operation. For more information, see the :manual:`insert command | ||
fields </reference/command/insert/#command-fields>` guide in the | ||
{+mdb-server+} manual. | ||
| **Type**: any valid BSON type | ||
|
||
Example | ||
~~~~~~~ | ||
|
||
The following code uses the ``insertMany()`` method to insert three new | ||
documents into a collection. Because the ``bypassDocumentValidation`` field | ||
is set to ``true`` in an options array, this | ||
insert operation bypasses document-level validation: | ||
|
||
.. literalinclude:: /includes/write/insert.php | ||
:language: php | ||
:dedent: | ||
:start-after: start-modify | ||
:end-before: end-modify | ||
|
||
Additional Information | ||
---------------------- | ||
|
||
.. TODO: | ||
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. Will this be addressed in a follow-up ticket? 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. Yes, there will be a final cleanup ticket that resolves all the TODOs |
||
For runnable code examples of inserting documents with the {+php-library+}, see | ||
:ref:`php-write`. | ||
|
||
API Documentation | ||
~~~~~~~~~~~~~~~~~ | ||
|
||
To learn more about any of the methods or types discussed in this | ||
guide, see the following API documentation: | ||
|
||
- `MongoDB\\Collection::insertOne() <{+api+}/method/MongoDBCollection-insertOne/>`__ | ||
- `MongoDB\\Collection::insertMany() <{+api+}/method/MongoDBCollection-insertMany/>`__ |
Oops, something went wrong.
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.
Personal opinion: with a list it's easier to read.