You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are a certain class of operations using the [findAndModify command]({{< docsref "reference/command/findAndModify/" >}}) from the server. This command will perform some operation on the document and return the document either before or after the modification takes place. By default, the document is returned before the modification takes place.
96
+
97
+
### FindOneAndDelete
98
+
99
+
A single document can be deleted atomically using [`FindOneAndDeleteAsync`]({{< apiref "M_MongoDB_Driver_IMongoCollection_1_FindOneAndDeleteAsync__1" >}}).
Assert(result["FirstName"] =="Jack"); // will be true if a document was found.
106
+
```
107
+
108
+
The above will find a document where the `FirstName` is `Jack` and delete it. It will then return the document that was just deleted.
109
+
110
+
111
+
### FindOneAndReplace
112
+
113
+
A single document can be replaced atomically using [`FindOneAndReplaceAsync`]({{< apiref "M_MongoDB_Driver_IMongoCollection_1_FindOneAndReplaceAsync__1" >}}).
Assert(result["FirstName"] =="Jack"); // will be true if a document was found.
122
+
```
123
+
124
+
The above will find a document where the `FirstName` is `Jack` and replace it with `replacementDocument`. It will then return the document that was replaced.
125
+
126
+
### FindOneAndUpdate
127
+
128
+
A single document can be updated atomically using [`FindOneAndUpdateAsync`]({{< apiref "M_MongoDB_Driver_IMongoCollection_1_FindOneAndUpdateAsync__1" >}}).
Assert(result["FirstName"] =="Jack"); // will be true if a document was found.
137
+
```
138
+
139
+
The above will find a document where the `FirstName` is `Jack` and set its `FirstName` field to `John`. It will then return the document that was replaced.
140
+
141
+
142
+
### Options
143
+
144
+
Each of the 3 operations has certain options available.
145
+
146
+
#### ReturnDocument
147
+
148
+
For Replacing and Updating, the [`ReturnDocument`]({{< apiref "T_MongoDB_Driver_ReturnDocument" >}}) enum can be provided. It allows you to return the document after the modification has taken place. The default is `Before`.
Assert(result["FirstName"] =="John"); // will be true if a document was found.
163
+
```
164
+
165
+
#### Projection
166
+
167
+
A projection can be provided to shape the result. The easiest way to build the projection is using a [projection builder]({{< relref "reference\driver\definitions.md#projections" >}}).
168
+
169
+
#### Sort
170
+
171
+
Since only a single document is selected, for filters that could result in multiple choices, a sort should be provided and the first document will be selected.
172
+
173
+
#### IsUpsert
174
+
175
+
For Replacing and Updating, `IsUpsert` can be specified such that, if the document isn't found, one will be inserted.
0 commit comments