1- .. _csharp-update-arrays:
2-
3- =============
4- Update Arrays
5- =============
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- :keywords: synchronous, asynchronous
19-
20- Overview
21- --------
22-
23- In this guide, you can learn how to update arrays in a document with the
24- MongoDB Java driver.
25-
26- To update an array, you must do the following:
27-
28- - Specify the update you want to perform
29- - Specify what array elements to apply your update to
30- - Perform an update operation using these specifications
31-
32- Sample Document
33- ~~~~~~~~~~~~~~~
34-
35- The following sections feature examples that update this sample
36- document:
37-
38- .. code-block:: json
39-
40- { "_id": 1, "color": "green", "qty": [8, 12, 18] }
41-
42- The examples on this page use the ``findOneAndUpdate()`` method of the
43- ``MongoCollection`` class to retrieve and update the document. Each
44- example uses an instance of the ``FindOneAndUpdateOptions`` class to
45- have MongoDB retrieve the document after the update occurs. For
46- more information on the ``findOneAndUpdate()`` method, see our
47- :doc:`Compound Operations guide </fundamentals/crud/compound-operations/>`.
48-
49- Specifying an Update
50- --------------------
51-
52- To specify an update, use the ``Updates`` builder. The ``Updates``
53- builder provides static utility methods to construct update
54- specifications. For more information about using the ``Updates`` builder with
55- arrays, see our :ref:`guide on the Updates builder <array_updates>`.
56-
57- The following example performs these actions:
58-
59- - Query for the sample document
60- - Append "17" to the ``qty`` array in the document that matches the query filter
61-
62- .. literalinclude:: /includes/fundamentals/code-snippets/UpdateArray.java
63- :language: java
64- :dedent:
65- :start-after: begin pushElementsExample
66- :end-before: end pushElementsExample
67-
68- The preceding example updates the original document to the following state:
69-
70- .. code-block:: json
71- :copyable: false
72-
73- { "_id": 1, "color": "green", "qty": [8, 12, 18, 17] }
74-
751Specifying Array Elements
762-------------------------
773
@@ -89,10 +15,6 @@ For additional information, see the Server Manual Entry on
8915The First Matching Array Element
9016~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9117
92- To update the first array element that matches your query filter, use the
93- positional ``$`` operator. The array field must appear as part of your
94- query filter to use the positional ``$`` operator.
95-
9618Example
9719```````
9820
0 commit comments