File tree 3 files changed +41
-3
lines changed
fundamentals/crud/write-operations
includes/code-examples/replace-one
3 files changed +41
-3
lines changed Original file line number Diff line number Diff line change @@ -109,8 +109,26 @@ corresponding code.
109
109
110
110
.. important::
111
111
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
+ }
114
132
115
133
Customize the Replace Operation
116
134
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Original file line number Diff line number Diff line change @@ -49,9 +49,14 @@ private static ReplaceOneResult ReplaceOneRestaurant()
49
49
var filter = Builders < Restaurant > . Filter
50
50
. Eq ( r => r . Cuisine , "Pizza" ) ;
51
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 ;
55
+
52
56
// Generates a new restaurant document
53
57
Restaurant newPizzaRestaurant = new ( )
54
58
{
59
+ Id = oldId
55
60
Name = "Mongo's Pizza" ,
56
61
Cuisine = "Pizza" ,
57
62
Address = new Address ( )
@@ -73,9 +78,14 @@ private static ReplaceOneResult ReplaceOneRestaurantWithOptions()
73
78
var filter = Builders < Restaurant > . Filter
74
79
. Eq ( r => r . Cuisine , "Pizza" ) ;
75
80
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
+
76
85
// Generates a new restaurant document
77
86
Restaurant newPizzaRestaurant = new ( )
78
87
{
88
+ Id = oldId
79
89
Name = "Mongo's Pizza" ,
80
90
Cuisine = "Pizza" ,
81
91
Address = new Address ( )
Original file line number Diff line number Diff line change @@ -48,10 +48,15 @@ private static async Task<ReplaceOneResult> ReplaceOneRestaurantAsync()
48
48
// start-replace-one-async
49
49
var filter = Builders < Restaurant > . Filter
50
50
. 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 ;
51
55
52
56
// Generates a new restaurant document
53
57
Restaurant newPizzaRestaurant = new ( )
54
- {
58
+ {
59
+ Id = oldId
55
60
Name = "Mongo's Pizza" ,
56
61
Cuisine = "Pizza" ,
57
62
Address = new Address ( )
@@ -72,9 +77,14 @@ private static async Task<ReplaceOneResult> ReplaceOneRestaurantAsyncWithOptions
72
77
var filter = Builders < Restaurant > . Filter
73
78
. Eq ( r => r . Cuisine , "Pizza" ) ;
74
79
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
+
75
84
// Generates a new restaurant document
76
85
Restaurant newPizzaRestaurant = new ( )
77
86
{
87
+ Id = oldId
78
88
Name = "Mongo's Pizza" ,
79
89
Cuisine = "Pizza" ,
80
90
Address = new Address ( )
You can’t perform that action at this time.
0 commit comments