@@ -20,14 +20,19 @@ Update Fields
20
20
Overview
21
21
--------
22
22
23
- In this guide, you can learn how to use the {+driver-long+} to modify
24
- documents in a MongoDB collection by performing update operations.
23
+ .. note:: Updating Arrays
25
24
26
- The {+driver-short+} provides the following methods to modify documents,
27
- each of which has an asynchronous and synchronous version:
25
+ This page discusses how to update document fields that contain single values.
26
+ To learn how to update array values in MongoDB documents, see
27
+ :ref:`<csharp-update-arrays>`.
28
28
29
- - ``UpdateOneAsync()`` or ``UpdateOne()``
30
- - ``UpdateManyAsync()`` or ``UpdateMany()``
29
+ In this guide, you can learn how to use the {+driver-long+} to update the
30
+ values of fields in MongoDB documents.
31
+
32
+ The {+driver-short+} provides the following methods to update fields:
33
+
34
+ - ``UpdateOne()`` or ``UpdateOneAsync()``
35
+ - ``UpdateMany()`` or ``UpdateManyAsync()``
31
36
32
37
.. tip:: Interactive Lab
33
38
@@ -70,47 +75,56 @@ and load this sample data.
70
75
71
76
.. _csharp-update-operation:
72
77
73
- Update Operations
74
- -----------------
78
+ Update One Document
79
+ -------------------
75
80
76
- You can perform update operations in MongoDB with the following methods:
81
+ To update one or more fields in a MongoDB document, call the ``UpdateOne()`` or
82
+ ``UpdateOneAsync()`` method. These methods accept the following parameters:
77
83
78
- - ``UpdateOne()``, which updates *the first document* that matches the search criteria
79
- - ``UpdateMany()``, which updates *all documents* that match the search criteria
84
+ .. list-table::
85
+ :widths: 30 70
86
+ :header-rows: 1
80
87
81
- Required Parameters
82
- ~~~~~~~~~~~~~~~~~~~
88
+ * - Parameter
89
+ - Description
83
90
84
- Each update method requires the following parameters:
91
+ * - ``filter``
92
+ - A *query filter* that specifies the document to update. You can use the
93
+ ``Builders`` class to create a query filter. For more information about
94
+ query filters, see the
95
+ :manual:`{+mdb-server+} manual </core/document/#query-filter-documents>`.
85
96
86
- - A **query filter** document, which determines which records to update. See the
87
- :manual:`MongoDB server manual </core/document/#query-filter-documents>` for
88
- more information about query filters.
89
- - An **update** document, which specifies the **update operator** (the kind of update to
90
- perform) and the fields and values that should change. See the
91
- :manual:`Field Update Operators Manual page</reference/operator/update-field/>` for a complete
92
- list of update operators and their usage.
97
+ **Data Type:** `FilterDefinition<TDocument> <{+new-api-root+>/MongoDB.Driver/MongoDB.Driver.FilterDefinition-1.html>`__
93
98
94
- The {+driver-short+} provides a ``Builders`` class that simplifies the creation of both
95
- query filters and update documents. The following code sample uses ``Builders`` to create
96
- two documents for use as parameters in an update operation:
99
+ * - ``update``
100
+ - An instance of the ``UpdateDefinition`` class. This object specifies the kind of update
101
+ operation, the fields to update, and the new value for each field. For a complete
102
+ list of update operations, see
103
+ :manual:`Field Update Operators </reference/operator/update-field/>` in the
104
+ {+mdb-server+} manual.
97
105
98
- - A query filter that searches for restaurants with a ``cuisine`` field value of "Pizza"
99
- - An update document that sets the value of the ``cuisine`` field of these restaurants
100
- to "Pasta and breadsticks"
106
+ **Data Type:** `UpdateDefinition<TDocument> <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.UpdateDefinition-1.html>`__
101
107
102
- .. literalinclude:: /includes/code-examples/update-many/UpdateMany.cs
103
- :language: csharp
104
- :dedent:
105
- :start-after: // start-update-many
106
- :end-before: // end-update-many
108
+ * - ``options``
109
+ - *Optional.* An instance of the ``UpdateOptions`` class that specifies the
110
+ configuration for the update operation. The default value is ``null``.
111
+
112
+ **Data Type:** `UpdateOptions <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.UpdateOptions.html>`__
113
+
114
+ * - ``cancellationToken``
115
+ - *Optional.* A token that you can use to cancel the operation.
116
+
117
+ **Data type**: `CancellationToken <https://learn.microsoft.com/dotnet/api/system.threading.cancellationtoken>`__
118
+
119
+ The following code example demonstrates how to perform an update operation. The
120
+ code performs the following steps:
107
121
108
122
.. tip:: Aggregation Pipelines in Update Operations
109
123
110
124
If you are using MongoDB Version 4.2 or later, you can use aggregation
111
125
pipelines made up of a subset of aggregation stages in update operations. For
112
126
more information on the aggregation stages MongoDB supports in
113
- aggregation pipelines used in update operations, see our tutorial on building
127
+ aggregation pipelines used in update operations, see the tutorial about building
114
128
:manual:`updates with aggregation pipelines </tutorial/update-documents-with-aggregation-pipeline/>`.
115
129
116
130
Update One Document
0 commit comments