-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathreplace.txt
271 lines (195 loc) · 9.02 KB
/
replace.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
.. _csharp-replace-operation:
.. _csharp-replace-documents:
=================
Replace Documents
=================
.. contents:: On this page
:local:
:backlinks: none
:depth: 2
:class: singlecol
.. facet::
:name: genre
:values: reference
.. meta::
:keywords: replace, synchronous, asynchronous
Overview
--------
In this guide, you can learn how to use the {+driver-short+} to replace
documents in a MongoDB collection.
The {+driver-short+} provides the ``ReplaceOne()`` and ``ReplaceOneAsync()`` methods.
These methods remove all fields (except the ``_id`` field) from the first document that
matches the search criteria, then insert the fields and values you specify into the
document.
.. include:: /includes/method-overloads.rst
.. include:: /includes/atlas-sample-data.rst
Replace One Document
--------------------
To replace a document in a collection, call the ``ReplaceOne()`` or ``ReplaceOneAsync()``
method. These methods accept the following parameters:
.. list-table::
:widths: 30 70
:header-rows: 1
* - Parameter
- Description
* - ``filter``
- A *query filter* that specifies the document to replace. You can use the
``Builders`` class to create a query filter. For more information about
query filters, see the
:manual:`{+mdb-server+} manual </core/document/#query-filter-documents>`.
**Data Type:** `FilterDefinition<TDocument> <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.FilterDefinition-1.html>`__
* - ``replacement``
- A *replacement* document, which specifies the fields and values to insert in the new
document. If the documents in your collection are mapped to a {+language+} class,
the replacement document can be an instance of this class.
**Data Type:** ``TDocument``
* - ``options``
- *Optional.* An instance of the ``ReplaceOptions`` class that specifies the
configuration for the replace operation. The default value is ``null``.
**Data Type:** `ReplaceOptions <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.ReplaceOptions.html>`__
* - ``cancellationToken``
- *Optional.* A token that you can use to cancel the operation.
**Data type**: `CancellationToken <https://learn.microsoft.com/dotnet/api/system.threading.cancellationtoken>`__
The following code example demonstrates how to perform a replace operation. The
code performs the following steps:
1. Creates a query filter by using the ``Builders`` class. The filter matches all
documents where the ``cuisine`` field has the value ``"Pizza"``.
#. Creates a new ``Restaurant`` object.
#. Calls the ``ReplaceOne()`` method on the ``restaurants`` collection. This operation
finds the first matching document in the collection and replaces it with the newly created
document.
Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the
corresponding code.
.. tabs::
.. tab:: Synchronous
:tabid: sync
.. literalinclude:: /includes/code-examples/replace-one/ReplaceOne.cs
:language: csharp
:copyable: true
:dedent:
:start-after: // start-replace-one
:end-before: // end-replace-one
.. tab:: Asynchronous
:tabid: async
.. literalinclude:: /includes/code-examples/replace-one/ReplaceOneAsync.cs
:language: csharp
:copyable: true
:dedent:
:start-after: // start-replace-one-async
:end-before: // end-replace-one-async
.. important::
The values of ``_id`` fields are immutable. If your replacement document
specifies a value for the ``_id`` field, it must match the ``_id`` value of
the existing document.
If your replacement document does not specify a value for the ``_id`` field,
you can add the ``[BsonIgnoreIfDefault]`` attribute to the ``_id`` field in
your Plain Old CLR/Class Object (POCO). Use ``[BsonIgnoreIfDefault]`` if the
``_id`` field in your POCO is of the ``ObjectId`` type.
The following example shows how to add this attribute:
.. code-block:: csharp
public class Restaurant
{
[BsonIgnoreIfDefault]
public ObjectId Id { get; set; }
// Other properties
}
Customize the Replace Operation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The ``ReplaceOne()`` and ``ReplaceOneAsync()`` methods optionally accept a
``ReplaceOptions`` object as a parameter, which represents options you can use to
configure the replace operation.
The ``ReplaceOptions`` class contains the following properties:
.. list-table::
:widths: 30 70
:header-rows: 1
* - Property
- Description
* - ``BypassDocumentValidation``
- Specifies whether the replace operation bypasses document validation. This lets you
replace documents that don't meet the schema validation requirements, if any
exist. See :manual:`the {+mdb-server+} manual </core/schema-validation/#schema-validation>`
for more information on schema validation.
**Data Type:** ``bool?``
* - ``Collation``
- Specifies the kind of language collation to use when sorting
results. See :manual:`the {+mdb-server+} manual </reference/collation/#std-label-collation>`
for more information on collation.
**Data Type:** `Collation <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.Collation.html>`__
* - ``Comment``
- Gets or sets the user-provided comment for the operation.
See :manual:`the {+mdb-server+} manual</reference/command/update/#command-fields>`
for more information.
**Data Type:** `BsonValue <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.BsonValue.html>`__
* - ``Hint``
- Gets or sets the index to use to scan for documents.
See :manual:`the {+mdb-server+} manual</reference/command/update/#std-label-update-command-hint>`
for more information.
**Data Type:** `BsonValue <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.BsonValue.html>`__
* - ``IsUpsert``
- Specifies whether the replace operation performs an upsert operation if no
documents match the query filter.
See :manual:`the {+mdb-server+} manual </reference/command/update/#std-label-update-command-upsert>`
for more information.
**Data Type:** ``bool``
* - ``Let``
- Gets or sets the let document.
See :manual:`the {+mdb-server+} manual </reference/command/update/#std-label-update-let-syntax>`
for more information.
**Data Type:** `BsonDocument <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.BsonDocument.html>`__
The following example performs the same steps as the preceding example, but also uses
the ``BypassDocumentValidation`` option to bypass any schema validation requirements.
Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding
code.
.. tabs::
.. tab:: Synchronous
:tabid: sync
.. literalinclude:: /includes/code-examples/replace-one/ReplaceOne.cs
:language: csharp
:copyable: true
:dedent:
:start-after: // start-replace-one-sync-with-options
:end-before: // end-replace-one-sync-with-options
.. tab:: Asynchronous
:tabid: async
.. literalinclude:: /includes/code-examples/replace-one/ReplaceOneAsync.cs
:language: csharp
:copyable: true
:dedent:
:start-after: // start-replace-one-async-with-options
:end-before: // end-replace-one-async-with-options
Return Value
~~~~~~~~~~~~
The ``ReplaceOne()`` method returns a ``ReplaceOneResult``
object, and the ``ReplaceOneAsync()`` method returns a ``Task<ReplaceOneResult>`` object.
The ``ReplaceOneResult`` class contains the following properties:
.. list-table::
:widths: 30 70
:header-rows: 1
* - Property
- Description
* - ``IsAcknowledged``
- Indicates whether the replace operation was acknowledged by MongoDB.
**Data Type:** ``bool``
* - ``IsModifiedCountAvailable``
- Indicates whether you can read the count of replaced records on the
``ReplaceOneResult``.
**Data Type:** ``bool``
* - ``MatchedCount``
- The number of documents that matched the query filter, regardless of
whether one was replaced.
**Data Type:** ``long``
* - ``ModifiedCount``
- The number of documents replaced by the replace operation.
**Data Type:** ``long``
* - ``UpsertedId``
- The ID of the document that was upserted in the database, if the driver
performed an upsert.
**Data Type:** `BsonValue <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.BsonValue.html>`__
API Documentation
~~~~~~~~~~~~~~~~~
To learn more about any of the methods and classes used on this page,
see the following API documentation:
* `ReplaceOne() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.IMongoCollection-1.ReplaceOne.html>`__
* `ReplaceOneAsync() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.IMongoCollection-1.ReplaceOneAsync.html>`__
* `ReplaceOptions <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.ReplaceOptions.html>`__
* `ReplaceOneResult <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.ReplaceOneResult.html>`__