4
4
Write Data to MongoDB
5
5
=====================
6
6
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
+
7
21
.. toctree::
8
22
:titlesonly:
9
23
:maxdepth: 1
@@ -12,3 +26,161 @@ Write Data to MongoDB
12
26
/write/replace
13
27
/write/insert
14
28
/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