Skip to content

Commit 4e1994b

Browse files
authored
DOCSP-41966: Write operations landing (#135)
* DOCSP-41966: Write operations landing * edits * RR feedback * JT feedback * JT feedback 2 * gridfs examples
1 parent a040fa8 commit 4e1994b

File tree

3 files changed

+289
-0
lines changed

3 files changed

+289
-0
lines changed

snooty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ toc_landing_pages = [
1818
"/reference/class/MongoDBModelDatabaseInfo",
1919
"/reference/class/MongoDBModelIndexInfo",
2020
"/get-started",
21+
"/write",
2122
]
2223

2324
[substitutions]
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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 values
9+
// start-insert-one
10+
$result = $collection->insertOne([
11+
'<field name 1>' => '<value 1>',
12+
'<field name 2>' => '<value 2>',
13+
]);
14+
// end-insert-one
15+
16+
// Inserts multiple documents that store the specified values
17+
// start-insert-multiple
18+
$result = $collection->insertMany(
19+
[
20+
'<field name 1>' => '<value 1>',
21+
'<field name 2>' => '<value 2>',
22+
],
23+
[
24+
'<field name 1>' => '<value 1>',
25+
'<field name 2>' => '<value 2>',
26+
],
27+
);
28+
// end-insert-multiple
29+
30+
// Updates a document that matches the specified criteria
31+
// start-update-one
32+
$result = $collection->updateOne(
33+
['<field to match>' => '<value to match>'],
34+
['$set' => ['<field name>' => '<value>']],
35+
);
36+
// end-update-one
37+
38+
// Updates all documents that match the specified criteria
39+
// start-update-multiple
40+
$result = $collection->updateMany(
41+
['<field to match>' => '<value to match>'],
42+
['$set' => ['<field name>' => '<value>']],
43+
);
44+
// end-update-multiple
45+
46+
// start-replace-one
47+
$result = $collection->replaceOne(
48+
['<field to match>' => '<value to match>'],
49+
[
50+
'<new field 1>' => '<value 1>',
51+
'<new field 2>' => '<value 2>',
52+
],
53+
);
54+
// end-replace-one
55+
56+
// Deletes a document that matches the specified criteria
57+
// start-delete-one
58+
$result = $collection->deleteOne(['<field name>' => '<value>']);
59+
// end-delete-one
60+
61+
// Deletes all documents that match the specified criteria
62+
// start-delete-multiple
63+
$result = $collection->deleteMany(['<field name>' => '<value>']);
64+
// end-delete-multiple
65+
66+
// Runs a bulk operation based on the instructions in each array entry
67+
// start-bulk-write
68+
$result = $collection->bulkWrite(
69+
[
70+
[
71+
'insertOne' => [
72+
['<field name>' => '<value>'],
73+
],
74+
],
75+
[
76+
'replaceOne' => [
77+
['<field to match>' => '<value to match>'],
78+
[
79+
'<first new field>' => '<value>',
80+
'<second new field>' => '<value>',
81+
],
82+
],
83+
],
84+
[
85+
'updateOne' => [
86+
['<field to match>' => '<value to match>'],
87+
['$set' => ['<field to update>' => '<value to update>']],
88+
],
89+
],
90+
[
91+
'updateMany' => [
92+
['<field to match>' => '<value to match>'],
93+
['$set' => ['<field to update>' => '<value to update>']],
94+
],
95+
],
96+
[
97+
'deleteOne' => [
98+
['<field name>' => '<value>'],
99+
],
100+
],
101+
[
102+
'deleteMany' => [
103+
['<field name>' => '<value>'],
104+
],
105+
],
106+
]
107+
);
108+
// end-bulk-write
109+
110+
// Stores a file in a GridFS bucket and writes data to the file
111+
// start-gridfs-upload
112+
$bucket = $client->selectDatabase('<database name>')->selectGridFSBucket();
113+
$stream = $bucket->openUploadStream('<file name>');
114+
fwrite($stream, '<data>');
115+
fclose($stream);
116+
// end-gridfs-upload

source/write.txt

Lines changed: 172 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,161 @@ 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 store files in a GridFS bucket by
178+
creating an upload stream:
179+
180+
.. literalinclude:: /includes/usage-examples/write-code-examples.php
181+
:start-after: start-gridfs-upload
182+
:end-before: end-gridfs-upload
183+
:language: php
184+
:dedent:
185+
186+
To learn more about GridFS, see the :ref:`Store Large Files <php-gridfs>` guide.

0 commit comments

Comments
 (0)