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 @@ -52,9 +52,14 @@ private static ReplaceOneResult ReplaceOneRestaurant()
52
52
var filter = Builders < Restaurant > . Filter
53
53
. Eq ( r => r . Cuisine , "Pizza" ) ;
54
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 ;
58
+
55
59
// Generates a new restaurant document
56
60
Restaurant newPizzaRestaurant = new ( )
57
61
{
62
+ Id = oldId
58
63
Name = "Mongo's Pizza" ,
59
64
Cuisine = "Pizza" ,
60
65
Address = new Address ( )
@@ -77,9 +82,14 @@ private static ReplaceOneResult ReplaceOneRestaurantWithOptions()
77
82
var filter = Builders < Restaurant > . Filter
78
83
. Eq ( r => r . Cuisine , "Pizza" ) ;
79
84
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
+
80
89
// Generates a new restaurant document
81
90
Restaurant newPizzaRestaurant = new ( )
82
91
{
92
+ Id = oldId
83
93
Name = "Mongo's Pizza" ,
84
94
Cuisine = "Pizza" ,
85
95
Address = new Address ( )
Original file line number Diff line number Diff line change @@ -51,10 +51,15 @@ private static async Task<ReplaceOneResult> ReplaceOneRestaurantAsync()
51
51
// Creates a filter for all restaurant documents that have a "cuisine" value of "Pizza"
52
52
var filter = Builders < Restaurant > . Filter
53
53
. 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 ;
54
58
55
59
// Generates a new restaurant document
56
60
Restaurant newPizzaRestaurant = new ( )
57
- {
61
+ {
62
+ Id = oldId
58
63
Name = "Mongo's Pizza" ,
59
64
Cuisine = "Pizza" ,
60
65
Address = new Address ( )
@@ -76,9 +81,14 @@ private static async Task<ReplaceOneResult> ReplaceOneRestaurantAsyncWithOptions
76
81
var filter = Builders < Restaurant > . Filter
77
82
. Eq ( r => r . Cuisine , "Pizza" ) ;
78
83
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
+
79
88
// Generates a new restaurant document
80
89
Restaurant newPizzaRestaurant = new ( )
81
90
{
91
+ Id = oldId
82
92
Name = "Mongo's Pizza" ,
83
93
Cuisine = "Pizza" ,
84
94
Address = new Address ( )
You can’t perform that action at this time.
0 commit comments