@@ -20,52 +20,22 @@ Update Arrays
20
20
Overview
21
21
--------
22
22
23
- On this page, you can learn how to use the {+driver-long+} to update array
24
- fields in MongoDB documents. This page describes how to create ``UpdateDefinition<TDocument>``
25
- objects that specify the update operations you want to perform on array fields.
26
- You can pass these objects to the update methods described on the
27
- :ref:`<csharp-update-documents>` page.
23
+ On this page, you can learn how to create ``UpdateDefinition`` objects for array fields.
24
+ An ``UpdateDefinition`` object specifies the kind of update operation to perform, the
25
+ fields to update, and the new value for each field, if applicable.
28
26
29
27
The {+driver-short+} supports the array update operators and modifiers described in the
30
28
:manual:`{+mdb-server+} manual </reference/operator/update/#array>`.
31
- To specify an update operation, call the corresponding method from the ``Builders.Update``
32
- property. The following sections describe these methods in more detail.
29
+ To create an ``UpdateDefinition`` object for one of these operators, call the corresponding
30
+ method from the ``Builders.Update`` property.
31
+ The following sections describe these methods in more detail.
33
32
34
- .. note:: Method Overloads
33
+ After you create an ``UpdateDefinition``, pass it to one of the update methods
34
+ described on the :ref:`<csharp-update-documents>` page.
35
35
36
- Many of the methods on this page have multiple overloads. The examples
37
- in this guide show only one overload for each method. For
38
- more information about the available overloads, see the
39
- :manual:`{+new-api-root+} API documentation </api-reference>`.
36
+ .. include:: /includes/method-overloads.rst
40
37
41
- Sample Data
42
- ~~~~~~~~~~~
43
-
44
- The examples in this guide use the ``restaurants`` collection
45
- from the ``sample_restaurants`` database. The documents in this
46
- collection use the following ``Restaurant``, ``Address``, and ``GradeEntry``
47
- classes as models:
48
-
49
- .. literalinclude:: /includes/code-examples/Restaurant.cs
50
- :language: csharp
51
- :copyable:
52
- :dedent:
53
-
54
- .. literalinclude:: /includes/code-examples/Address.cs
55
- :language: csharp
56
- :copyable:
57
- :dedent:
58
-
59
- .. literalinclude:: /includes/code-examples/GradeEntry.cs
60
- :language: csharp
61
- :copyable:
62
- :dedent:
63
-
64
- .. include:: /includes/convention-pack-note.rst
65
-
66
- This collection is from the :atlas:`sample datasets </sample-data>` provided
67
- by Atlas. See the :ref:`<csharp-quickstart>` to learn how to create a free MongoDB cluster
68
- and load this sample data.
38
+ .. include:: /includes/atlas-sample-data.rst
69
39
70
40
Add One Value
71
41
-------------
@@ -90,35 +60,64 @@ This method accepts the following parameters:
90
60
91
61
**Data Type:** ``TItem``
92
62
93
- The following code example pushes a new ``GradeEntry`` object into the ``Grades``
94
- array in the matching document. Select the :guilabel:`Synchronous` or
95
- :guilabel:`Asynchronous` tab to see the corresponding code.
63
+ The following code example uses the ``Push()`` method to add a new ``GradeEntry`` object
64
+ to the ``Grades`` array in the matching documents:
96
65
97
66
.. tabs::
98
67
99
- .. tab:: Synchronous
100
- :tabid: update-one-array -sync
68
+ .. tab:: UpdateOne (Sync)
69
+ :tabid: update-one-push -sync
101
70
102
- .. literalinclude:: /includes/code-examples/update-one/UpdateOne .cs
71
+ .. literalinclude:: /includes/code-examples/UpdateArrays .cs
103
72
:language: csharp
104
73
:copyable: true
105
74
:dedent:
106
- :start-after: // start-update-one-array
107
- :end-before: // end-update-one-array
75
+ :start-after: // start-update-one-push
76
+ :end-before: // end-update-one-push
108
77
109
- .. tab:: Asynchronous
110
- :tabid: update-one-array -async
78
+ .. tab:: UpdateOne (Async)
79
+ :tabid: update-one-push -async
111
80
112
- .. literalinclude:: /includes/code-examples/update-one/UpdateOne .cs
81
+ .. literalinclude:: /includes/code-examples/UpdateArrays .cs
113
82
:language: csharp
114
83
:copyable: true
115
84
:dedent:
116
- :start-after: // start-update-one-array-async
117
- :end-before: // end-update-one-array-async
85
+ :start-after: // start-update-one-push-async
86
+ :end-before: // end-update-one-push-async
87
+
88
+ .. tab:: UpdateMany (Sync)
89
+ :tabid: update-many-push-sync
90
+
91
+ .. literalinclude:: /includes/code-examples/UpdateArrays.cs
92
+ :language: csharp
93
+ :copyable: true
94
+ :dedent:
95
+ :start-after: // start-update-many-push
96
+ :end-before: // end-update-many-push
97
+
98
+ .. tab:: UpdateMany (Async)
99
+ :tabid: update-many-push-async
100
+
101
+ .. literalinclude:: /includes/code-examples/UpdateArrays.cs
102
+ :language: csharp
103
+ :copyable: true
104
+ :dedent:
105
+ :start-after: // start-update-many-push-async
106
+ :end-before: // end-update-many-push-async
107
+
108
+ .. tip:: Configure the Push Operation
109
+
110
+ To add a value at a specific position in an array, or to sort or slice the array after
111
+ updating it, call the ``PushEach()`` method instead.
112
+ TODO: link this
118
113
119
114
To add one value to the end of an array, *but only if it doesn't already exist in the array*,
120
- call the ``Builders.Update.AddToSet()`` method.
121
- This method accepts the following parameters:
115
+ call the ``Builders.Update.AddToSet()`` method.
116
+ {+mdb-server+} determines whether the value already exists in the array by
117
+ comparing the value's BSON representation to the BSON representation of each
118
+ other element in the array.
119
+
120
+ The ``AddToSet()`` method accepts the following parameters:
122
121
123
122
.. list-table::
124
123
:widths: 30 70
@@ -137,13 +136,97 @@ This method accepts the following parameters:
137
136
138
137
**Data Type:** ``TItem``
139
138
140
- The following code example pushes a new ``GradeEntry `` object into the ``Grades``
141
- array in the matching document. Select the :guilabel:`Synchronous` or
142
- :guilabel:`Asynchronous` tab to see the corresponding code .
139
+ The following code example tries to use the ``AddToSet() `` method to re-add the first
140
+ ``GradeEntry`` object to the ``Grades`` array in the matching documents. Because
141
+ the value already exists in the array, the update operation does nothing .
143
142
144
- .. tip:: Specify a Position
145
-
146
- To add a value at a specific position in an array, call the ``PushEach()`` method.
143
+ .. tabs::
144
+
145
+ .. tab:: UpdateOne (Sync)
146
+ :tabid: update-one-addtoset-sync
147
+
148
+ .. literalinclude:: /includes/code-examples/UpdateArrays.cs
149
+ :language: csharp
150
+ :copyable: true
151
+ :dedent:
152
+ :start-after: // start-update-one-addtoset
153
+ :end-before: // end-update-one-addtoset
154
+
155
+ .. tab:: UpdateOne (Async)
156
+ :tabid: update-one-addtoset-async
157
+
158
+ .. literalinclude:: /includes/code-examples/UpdateArrays.cs
159
+ :language: csharp
160
+ :copyable: true
161
+ :dedent:
162
+ :start-after: // start-update-one-addtoset-async
163
+ :end-before: // end-update-one-addtoset-async
164
+
165
+ .. tab:: UpdateMany (Sync)
166
+ :tabid: update-many-addtoset-sync
167
+
168
+ .. literalinclude:: /includes/code-examples/UpdateArrays.cs
169
+ :language: csharp
170
+ :copyable: true
171
+ :dedent:
172
+ :start-after: // start-update-many-addtoset
173
+ :end-before: // end-update-many-addtoset
174
+
175
+ .. tab:: UpdateMany (Async)
176
+ :tabid: update-many-addtoset-async
177
+
178
+ .. literalinclude:: /includes/code-examples/UpdateArrays.cs
179
+ :language: csharp
180
+ :copyable: true
181
+ :dedent:
182
+ :start-after: // start-update-many-addtoset-async
183
+ :end-before: // end-update-many-addtoset-async
184
+
185
+ The following code example uses the ``PushEach()`` method to add two new ``GradeEntry``
186
+ objects to the start of the ``Grades`` array. It then sorts the array elements in
187
+ descending order by the values of their ``Score`` fields.
188
+
189
+ .. tabs::
190
+
191
+ .. tab:: UpdateOne (Sync)
192
+ :tabid: update-one-pusheach-sync
193
+
194
+ .. literalinclude:: /includes/code-examples/UpdateArrays.cs
195
+ :language: csharp
196
+ :copyable: true
197
+ :dedent:
198
+ :start-after: // start-update-one-pusheach
199
+ :end-before: // end-update-one-pusheach
200
+
201
+ .. tab:: UpdateOne (Async)
202
+ :tabid: update-one-pusheach-async
203
+
204
+ .. literalinclude:: /includes/code-examples/UpdateArrays.cs
205
+ :language: csharp
206
+ :copyable: true
207
+ :dedent:
208
+ :start-after: // start-update-one-pusheach-async
209
+ :end-before: // end-update-one-pusheach-async
210
+
211
+ .. tab:: UpdateMany (Sync)
212
+ :tabid: update-many-pusheach-sync
213
+
214
+ .. literalinclude:: /includes/code-examples/UpdateArrays.cs
215
+ :language: csharp
216
+ :copyable: true
217
+ :dedent:
218
+ :start-after: // start-update-many-pusheach
219
+ :end-before: // end-update-many-pusheach
220
+
221
+ .. tab:: UpdateMany (Async)
222
+ :tabid: update-many-pusheach-async
223
+
224
+ .. literalinclude:: /includes/code-examples/UpdateArrays.cs
225
+ :language: csharp
226
+ :copyable: true
227
+ :dedent:
228
+ :start-after: // start-update-many-pusheach-async
229
+ :end-before: // end-update-many-pusheach-async
147
230
148
231
Add Multiple Values
149
232
-------------------
@@ -169,20 +252,23 @@ This method accepts the following parameters:
169
252
**Data Type:** ``IEnumerable<TItem>``
170
253
171
254
* - ``slice``
172
- - The number of elements to keep in the array. If the value is negative, the
173
- method keeps the specified number of elements from the end of the array.
255
+ - The number of elements to keep in the array, counted from the start of the array
256
+ after updates. If the value is negative,
257
+ the method keeps the specified number of elements from the end of the array.
174
258
175
259
**Data Type:** ``int?``
176
260
177
261
* - ``position``
178
- - The position in the array at which to add the values.
262
+ - The position in the array at which to add the values. By default, the method
263
+ adds the values to the end of the array.
179
264
180
265
**Data Type:** ``int?``
181
266
182
267
* - ``sort``
183
- - The values to add to the array field.
268
+ - A ``SortDefinition`` object that specifies how the driver sorts the array elements
269
+ after adding the new values.
184
270
185
- **Data Type:** `SortDefinition<TItem> <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.SortDefinition-1.html>`__
271
+ **Data Type:** `SortDefinition<TItem> <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.SortDefinition-1.html>`__
186
272
187
273
To add multiple values to an array only if they aren't there, call the
188
274
``Builders.Update.AddToSetEach()`` method. This method accepts the following parameters:
@@ -204,6 +290,11 @@ To add multiple values to an array only if they aren't there, call the
204
290
205
291
**Data Type:** ``IEnumerable<TItem>``
206
292
293
+ .. tip::
294
+
295
+ If you call the ``AddToSet()`` method with more than one value, the driver automatically
296
+ performs the operation as if you called the ``AddToSetEach()`` method.
297
+
207
298
Remove Values
208
299
-------------
209
300
0 commit comments