Skip to content

Commit 46c134b

Browse files
stephmarie17github-actions[bot]
authored andcommitted
DOCSP-46303 Update replace documents (#406)
(cherry picked from commit a9ad799)
1 parent d832674 commit 46c134b

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
@@ -49,9 +49,14 @@ private static ReplaceOneResult ReplaceOneRestaurant()
4949
var filter = Builders<Restaurant>.Filter
5050
.Eq(r => r.Cuisine, "Pizza");
5151

52+
// Finds the ID of the first restaurant document that matches the filter
53+
var oldPizzaRestaurant = _restaurantsCollection.Find(filter).First();
54+
var oldId = oldPizzaRestaurant.Id;
55+
5256
// Generates a new restaurant document
5357
Restaurant newPizzaRestaurant = new()
5458
{
59+
Id = oldId
5560
Name = "Mongo's Pizza",
5661
Cuisine = "Pizza",
5762
Address = new Address()
@@ -73,9 +78,14 @@ private static ReplaceOneResult ReplaceOneRestaurantWithOptions()
7378
var filter = Builders<Restaurant>.Filter
7479
.Eq(r => r.Cuisine, "Pizza");
7580

81+
// Finds the ID of the first restaurant document that matches the filter
82+
var oldPizzaRestaurant = _restaurantsCollection.Find(filter).First();
83+
var oldId = oldPizzaRestaurant.Id;
84+
7685
// Generates a new restaurant document
7786
Restaurant newPizzaRestaurant = new()
7887
{
88+
Id = oldId
7989
Name = "Mongo's Pizza",
8090
Cuisine = "Pizza",
8191
Address = new Address()

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,15 @@ private static async Task<ReplaceOneResult> ReplaceOneRestaurantAsync()
4848
// start-replace-one-async
4949
var filter = Builders<Restaurant>.Filter
5050
.Eq(r => r.Cuisine, "Pizza");
51+
52+
// Finds the ID of the first restaurant document that matches the filter
53+
var oldPizzaRestaurant = _restaurantsCollection.Find(filter).First();
54+
var oldId = oldPizzaRestaurant.Id;
5155

5256
// Generates a new restaurant document
5357
Restaurant newPizzaRestaurant = new()
54-
{
58+
{
59+
Id = oldId
5560
Name = "Mongo's Pizza",
5661
Cuisine = "Pizza",
5762
Address = new Address()
@@ -72,9 +77,14 @@ private static async Task<ReplaceOneResult> ReplaceOneRestaurantAsyncWithOptions
7277
var filter = Builders<Restaurant>.Filter
7378
.Eq(r => r.Cuisine, "Pizza");
7479

80+
// Finds the ID of the first restaurant document that matches the filter
81+
var oldPizzaRestaurant = _restaurantsCollection.Find(filter).First();
82+
var oldId = oldPizzaRestaurant.Id;
83+
7584
// Generates a new restaurant document
7685
Restaurant newPizzaRestaurant = new()
7786
{
87+
Id = oldId
7888
Name = "Mongo's Pizza",
7989
Cuisine = "Pizza",
8090
Address = new Address()

0 commit comments

Comments
 (0)