Skip to content

Commit 7e86fa3

Browse files
committed
DOCSP-41966: Write operations landing
1 parent 55a44c1 commit 7e86fa3

File tree

2 files changed

+277
-0
lines changed

2 files changed

+277
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
require 'vendor/autoload.php';
3+
4+
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
5+
$client = new MongoDB\Client($uri);
6+
$collection = $client->db->coll;
7+
8+
// Inserts one document that stores the specified value
9+
// start-insert-one
10+
$result = $collection->insertOne(['<field name>' => '<value>']);
11+
// end-insert-one
12+
13+
// Inserts multiple documents that store the specified values
14+
// start-insert-multiple
15+
$result = $collection->insertMany(
16+
['<field name>' => '<value>'],
17+
['<field name>' => '<value>'],
18+
);
19+
// end-insert-multiple
20+
21+
// Updates a document that matches the specified criteria
22+
// start-update-one
23+
$result = $collection->updateOne(
24+
['<field to match>' => '<value to match>'],
25+
['$set' => ['<field name>' => '<value>']],
26+
);
27+
// end-update-one
28+
29+
// Updates all documents that match the specified criteria
30+
// start-update-multiple
31+
$result = $collection->updateMany(
32+
['<field to match>' => '<value to match>'],
33+
['$set' => ['<field name>' => '<value>']],
34+
);
35+
// end-update-multiple
36+
37+
// start-replace-one
38+
$result = $collection->replaceOne(
39+
['<field to match>' => '<value to match>'],
40+
[
41+
'<first new field>' => '<value>',
42+
'<second new field>' => '<value>',
43+
],
44+
);
45+
// end-replace-one
46+
47+
// Deletes a document that matches the specified criteria
48+
// start-delete-one
49+
$result = $collection->deleteOne(['<field name>' => '<value>']);
50+
// end-delete-one
51+
52+
// Deletes all documents that match the specified criteria
53+
// start-delete-multiple
54+
$result = $collection->deleteMany(['<field name>' => '<value>']);
55+
// end-delete-multiple
56+
57+
// Runs a bulk operation based on the instructions in each array entry
58+
// start-bulk-write
59+
$result = $collection->bulkWrite(
60+
[
61+
[
62+
'insertOne' => [
63+
['<field name>' => '<value>'],
64+
],
65+
],
66+
[
67+
'replaceOne' => [
68+
['<field to match>' => '<value to match>'],
69+
[
70+
'<first new field>' => '<value>',
71+
'<second new field>' => '<value>',
72+
],
73+
],
74+
],
75+
[
76+
'updateOne' => [
77+
['<field to match>' => '<value to match>'],
78+
['$set' => ['<field to update>' => '<value to update>']],
79+
],
80+
],
81+
[
82+
'updateMany' => [
83+
['<field to match>' => '<value to match>'],
84+
['$set' => ['<field to update>' => '<value to update>']],
85+
],
86+
],
87+
[
88+
'deleteOne' => [
89+
['<field name>' => '<value>'],
90+
],
91+
],
92+
[
93+
'deleteMany' => [
94+
['<field name>' => '<value>'],
95+
],
96+
],
97+
]
98+
);
99+
// end-bulk-write
100+
101+
// Creates a GridFS bucket or references an existing one
102+
// start-gridfs
103+
$bucket = $db->selectGridFSBucket();
104+
// end-gridfs

source/write.txt

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44
Write Data to MongoDB
55
=====================
66

7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
.. facet::
14+
:name: genre
15+
:values: reference
16+
17+
.. meta::
18+
:description: Learn how to use the PHP Library to write data to MongoDB.
19+
:keywords: usage examples, save, crud, create, code example
20+
721
.. toctree::
822
:titlesonly:
923
:maxdepth: 1
@@ -12,3 +26,162 @@ Write Data to MongoDB
1226
/write/replace
1327
/write/insert
1428
/write/update
29+
30+
Overview
31+
--------
32+
33+
On this page, you can see copyable code examples that show common
34+
{+php-library+} methods for writing data to MongoDB.
35+
36+
.. tip::
37+
38+
To learn more about any of the methods shown on this page, see the link
39+
provided in each section.
40+
41+
To use an example from this page, copy the code example into the
42+
:ref:`sample application <php-write-sample>` or your own application.
43+
Make sure to set the ``MONGODB_URI`` environment variable to the
44+
connection string for your MongoDB deployment, and replace the
45+
``<database>`` and ``<collection>`` placeholders with values for your
46+
target namespace.
47+
48+
.. _php-write-sample:
49+
50+
.. include:: /includes/usage-examples/sample-app-intro.rst
51+
52+
.. literalinclude:: /includes/usage-examples/sample-app.php
53+
:language: php
54+
:dedent:
55+
:linenos:
56+
:emphasize-lines: 10-12
57+
58+
Insert One
59+
----------
60+
61+
The following code shows how to insert a single document into a collection:
62+
63+
.. literalinclude:: /includes/usage-examples/write-code-examples.php
64+
:start-after: start-insert-one
65+
:end-before: end-insert-one
66+
:language: php
67+
:dedent:
68+
69+
To learn more about the ``MongoDB\Collection::insertOne()`` method, see the
70+
:ref:`Insert Documents <php-write-insert>` guide.
71+
72+
Insert Multiple
73+
---------------
74+
75+
The following code shows how to insert multiple documents into a collection:
76+
77+
.. literalinclude:: /includes/usage-examples/write-code-examples.php
78+
:start-after: start-insert-multiple
79+
:end-before: end-insert-multiple
80+
:language: php
81+
:dedent:
82+
83+
To learn more about the ``MongoDB\Collection::insertMany()`` method, see the
84+
:ref:`Insert Documents <php-write-insert>` guide.
85+
86+
Update One
87+
----------
88+
89+
The following code shows how to update a single document in a collection by creating
90+
or editing a field:
91+
92+
.. literalinclude:: /includes/usage-examples/write-code-examples.php
93+
:start-after: start-update-one
94+
:end-before: end-update-one
95+
:language: php
96+
:dedent:
97+
98+
To learn more about the ``MongoDB\Collection::updateOne()`` method, see the
99+
:ref:`Update Documents <php-write-update>` guide.
100+
101+
Update Multiple
102+
---------------
103+
104+
The following code shows how to update multiple documents in a collection by creating
105+
or editing a field:
106+
107+
.. literalinclude:: /includes/usage-examples/write-code-examples.php
108+
:start-after: start-update-multiple
109+
:end-before: end-update-multiple
110+
:language: php
111+
:dedent:
112+
113+
To learn more about the ``MongoDB\Collection::updateMany()`` method, see the
114+
:ref:`Update Documents <php-write-update>` guide.
115+
116+
Replace One
117+
-----------
118+
119+
The following code shows how to replace a single document in a collection
120+
with another document:
121+
122+
.. literalinclude:: /includes/usage-examples/write-code-examples.php
123+
:start-after: start-replace-one
124+
:end-before: end-replace-one
125+
:language: php
126+
:dedent:
127+
128+
To learn more about the ``MongoDB\Collection::replaceOne()`` method, see the
129+
:ref:`Replace Documents <php-write-replace>` guide.
130+
131+
Delete One
132+
----------
133+
134+
The following code shows how to delete a single document in a collection:
135+
136+
.. literalinclude:: /includes/usage-examples/write-code-examples.php
137+
:start-after: start-delete-one
138+
:end-before: end-delete-one
139+
:language: php
140+
:dedent:
141+
142+
To learn more about the ``MongoDB\Collection::deleteOne()`` method, see the
143+
:ref:`Delete Documents <php-write-delete>` guide.
144+
145+
Delete Multiple
146+
---------------
147+
148+
The following code shows how to delete multiple documents in a collection:
149+
150+
.. literalinclude:: /includes/usage-examples/write-code-examples.php
151+
:start-after: start-delete-multiple
152+
:end-before: end-delete-multiple
153+
:language: php
154+
:dedent:
155+
156+
To learn more about the ``MongoDB\Collection::deleteMany()`` method, see the
157+
:ref:`Delete Documents <php-write-delete>` guide.
158+
159+
Bulk Write
160+
----------
161+
162+
The following code shows how to perform multiple write operations in a single bulk
163+
operation:
164+
165+
.. literalinclude:: /includes/usage-examples/write-code-examples.php
166+
:start-after: start-bulk-write
167+
:end-before: end-bulk-write
168+
:language: php
169+
:dedent:
170+
171+
To learn more about the ``MongoDB\Collection::bulkWrite()`` method, see the
172+
:ref:`Bulk Write <php-bulk-write>` guide.
173+
174+
Store Large Files
175+
-----------------
176+
177+
The following code shows how to create or reference a GridFS bucket, which
178+
you can write large files to:
179+
180+
.. literalinclude:: /includes/usage-examples/write-code-examples.php
181+
:start-after: start-gridfs
182+
:end-before: end-gridfs
183+
:language: php
184+
:dedent:
185+
186+
To learn more about the ``MongoDB\Database::selectGridFSBucket()`` method, see the
187+
:ref:`Store Large Files <php-gridfs>` guide.

0 commit comments

Comments
 (0)