Skip to content

Commit d108049

Browse files
DOCSP-46303 Update replace documents (#406) (#419)
(cherry picked from commit a9ad799) Co-authored-by: Stephanie <[email protected]>
1 parent bae6c3b commit d108049

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

source/fundamentals/crud/write-operations/replace.txt

+20-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,26 @@ corresponding code.
109109

110110
.. important::
111111

112-
The values of ``_id`` fields are immutable. If your replacement document specifies
113-
a value for the ``_id`` field, it must match the ``_id`` value of the existing document.
112+
The values of ``_id`` fields are immutable. If your replacement document
113+
specifies a value for the ``_id`` field, it must match the ``_id`` value of
114+
the existing document.
115+
116+
If your replacement document does not specify a value for the ``_id`` field,
117+
you can add the ``[BsonIgnoreIfDefault]`` attribute to the ``_id`` field in
118+
your Plain Old CLR/Class Object (POCO). Use ``[BsonIgnoreIfDefault]`` if the
119+
``_id`` field in your POCO is of the ``ObjectId`` type.
120+
121+
The following example shows how to add this attribute:
122+
123+
.. code-block:: csharp
124+
125+
public class Restaurant
126+
{
127+
[BsonIgnoreIfDefault]
128+
public ObjectId Id { get; set; }
129+
130+
// Other properties
131+
}
114132

115133
Customize the Replace Operation
116134
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

source/includes/code-examples/replace-one/ReplaceOne.cs

+10
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,14 @@ private static ReplaceOneResult ReplaceOneRestaurant()
5252
var filter = Builders<Restaurant>.Filter
5353
.Eq(r => r.Cuisine, "Pizza");
5454

55+
// Finds the ID of the first restaurant document that matches the filter
56+
var oldPizzaRestaurant = _restaurantsCollection.Find(filter).First();
57+
var oldId = oldPizzaRestaurant.Id;
58+
5559
// Generates a new restaurant document
5660
Restaurant newPizzaRestaurant = new()
5761
{
62+
Id = oldId
5863
Name = "Mongo's Pizza",
5964
Cuisine = "Pizza",
6065
Address = new Address()
@@ -77,9 +82,14 @@ private static ReplaceOneResult ReplaceOneRestaurantWithOptions()
7782
var filter = Builders<Restaurant>.Filter
7883
.Eq(r => r.Cuisine, "Pizza");
7984

85+
// Finds the ID of the first restaurant document that matches the filter
86+
var oldPizzaRestaurant = _restaurantsCollection.Find(filter).First();
87+
var oldId = oldPizzaRestaurant.Id;
88+
8089
// Generates a new restaurant document
8190
Restaurant newPizzaRestaurant = new()
8291
{
92+
Id = oldId
8393
Name = "Mongo's Pizza",
8494
Cuisine = "Pizza",
8595
Address = new Address()

source/includes/code-examples/replace-one/ReplaceOneAsync.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,15 @@ private static async Task<ReplaceOneResult> ReplaceOneRestaurantAsync()
5151
// Creates a filter for all restaurant documents that have a "cuisine" value of "Pizza"
5252
var filter = Builders<Restaurant>.Filter
5353
.Eq(r => r.Cuisine, "Pizza");
54+
55+
// Finds the ID of the first restaurant document that matches the filter
56+
var oldPizzaRestaurant = _restaurantsCollection.Find(filter).First();
57+
var oldId = oldPizzaRestaurant.Id;
5458

5559
// Generates a new restaurant document
5660
Restaurant newPizzaRestaurant = new()
57-
{
61+
{
62+
Id = oldId
5863
Name = "Mongo's Pizza",
5964
Cuisine = "Pizza",
6065
Address = new Address()
@@ -76,9 +81,14 @@ private static async Task<ReplaceOneResult> ReplaceOneRestaurantAsyncWithOptions
7681
var filter = Builders<Restaurant>.Filter
7782
.Eq(r => r.Cuisine, "Pizza");
7883

84+
// Finds the ID of the first restaurant document that matches the filter
85+
var oldPizzaRestaurant = _restaurantsCollection.Find(filter).First();
86+
var oldId = oldPizzaRestaurant.Id;
87+
7988
// Generates a new restaurant document
8089
Restaurant newPizzaRestaurant = new()
8190
{
91+
Id = oldId
8292
Name = "Mongo's Pizza",
8393
Cuisine = "Pizza",
8494
Address = new Address()

0 commit comments

Comments
 (0)