Skip to content

Commit 0cae65f

Browse files
committed
added find and modify to the documentation.
1 parent a3f4323 commit 0cae65f

File tree

1 file changed

+85
-1
lines changed
  • Docs/reference/content/reference/driver/crud

1 file changed

+85
-1
lines changed

Docs/reference/content/reference/driver/crud/writing.md

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,88 @@ var result = await collection.DeleteOneAsync(filter);
8888
// and
8989
9090
var result = await collection.DeleteManyAsync(filter);
91-
```
91+
```
92+
93+
## Find And Modify
94+
95+
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" >}}).
100+
101+
```csharp
102+
var filter = new BsonDocument("FirstName", "Jack");
103+
var result = await collection.FindOneAndDeleteAsync(filter);
104+
105+
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" >}}).
114+
115+
```csharp
116+
var filter = new BsonDocument("FirstName", "Jack");
117+
var replacementDocument = new BsonDocument("FirstName", "John");
118+
119+
var result = await collection.FindOneAndReplaceAsync(filter, doc);
120+
121+
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" >}}).
129+
130+
```csharp
131+
var filter = new BsonDocument("FirstName", "Jack");
132+
var update = Builders<BsonDocument>.Update.Set("FirstName", "John");
133+
134+
var result = await collection.FindOneAndUpdateAsync(filter, update);
135+
136+
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`.
149+
150+
For example:
151+
152+
```csharp
153+
var filter = new BsonDocument("FirstName", "Jack");
154+
var update = Builders<BsonDocument>.Update.Set("FirstName", "John");
155+
156+
var options = new FindOneAndUpdateOptions<BsonDocument, BsonDocument>
157+
{
158+
ReturnDocument = ReturnDocument.After
159+
};
160+
var result = await collection.FindOneAndUpdateAsync(filter, update, options);
161+
162+
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

Comments
 (0)