From 46c134ba1e80390af1fcfe8a3aae184a790c586c Mon Sep 17 00:00:00 2001 From: Stephanie <52582720+stephmarie17@users.noreply.github.com> Date: Wed, 22 Jan 2025 13:35:43 -0800 Subject: [PATCH] DOCSP-46303 Update replace documents (#406) (cherry picked from commit a9ad799631a39700a0fc29b539f8016c8d9cfd11) --- .../crud/write-operations/replace.txt | 22 +++++++++++++++++-- .../code-examples/replace-one/ReplaceOne.cs | 10 +++++++++ .../replace-one/ReplaceOneAsync.cs | 12 +++++++++- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/source/fundamentals/crud/write-operations/replace.txt b/source/fundamentals/crud/write-operations/replace.txt index e64af0d9..34ef27e0 100644 --- a/source/fundamentals/crud/write-operations/replace.txt +++ b/source/fundamentals/crud/write-operations/replace.txt @@ -109,8 +109,26 @@ corresponding code. .. important:: - The values of ``_id`` fields are immutable. If your replacement document specifies - a value for the ``_id`` field, it must match the ``_id`` value of the existing document. + The values of ``_id`` fields are immutable. If your replacement document + specifies a value for the ``_id`` field, it must match the ``_id`` value of + the existing document. + + If your replacement document does not specify a value for the ``_id`` field, + you can add the ``[BsonIgnoreIfDefault]`` attribute to the ``_id`` field in + your Plain Old CLR/Class Object (POCO). Use ``[BsonIgnoreIfDefault]`` if the + ``_id`` field in your POCO is of the ``ObjectId`` type. + + The following example shows how to add this attribute: + + .. code-block:: csharp + + public class Restaurant + { + [BsonIgnoreIfDefault] + public ObjectId Id { get; set; } + + // Other properties + } Customize the Replace Operation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/source/includes/code-examples/replace-one/ReplaceOne.cs b/source/includes/code-examples/replace-one/ReplaceOne.cs index 592fb134..aff8367e 100644 --- a/source/includes/code-examples/replace-one/ReplaceOne.cs +++ b/source/includes/code-examples/replace-one/ReplaceOne.cs @@ -49,9 +49,14 @@ private static ReplaceOneResult ReplaceOneRestaurant() var filter = Builders.Filter .Eq(r => r.Cuisine, "Pizza"); + // Finds the ID of the first restaurant document that matches the filter + var oldPizzaRestaurant = _restaurantsCollection.Find(filter).First(); + var oldId = oldPizzaRestaurant.Id; + // Generates a new restaurant document Restaurant newPizzaRestaurant = new() { + Id = oldId Name = "Mongo's Pizza", Cuisine = "Pizza", Address = new Address() @@ -73,9 +78,14 @@ private static ReplaceOneResult ReplaceOneRestaurantWithOptions() var filter = Builders.Filter .Eq(r => r.Cuisine, "Pizza"); + // Finds the ID of the first restaurant document that matches the filter + var oldPizzaRestaurant = _restaurantsCollection.Find(filter).First(); + var oldId = oldPizzaRestaurant.Id; + // Generates a new restaurant document Restaurant newPizzaRestaurant = new() { + Id = oldId Name = "Mongo's Pizza", Cuisine = "Pizza", Address = new Address() diff --git a/source/includes/code-examples/replace-one/ReplaceOneAsync.cs b/source/includes/code-examples/replace-one/ReplaceOneAsync.cs index 677bc472..ba712a04 100644 --- a/source/includes/code-examples/replace-one/ReplaceOneAsync.cs +++ b/source/includes/code-examples/replace-one/ReplaceOneAsync.cs @@ -48,10 +48,15 @@ private static async Task ReplaceOneRestaurantAsync() // start-replace-one-async var filter = Builders.Filter .Eq(r => r.Cuisine, "Pizza"); + + // Finds the ID of the first restaurant document that matches the filter + var oldPizzaRestaurant = _restaurantsCollection.Find(filter).First(); + var oldId = oldPizzaRestaurant.Id; // Generates a new restaurant document Restaurant newPizzaRestaurant = new() - { + { + Id = oldId Name = "Mongo's Pizza", Cuisine = "Pizza", Address = new Address() @@ -72,9 +77,14 @@ private static async Task ReplaceOneRestaurantAsyncWithOptions var filter = Builders.Filter .Eq(r => r.Cuisine, "Pizza"); + // Finds the ID of the first restaurant document that matches the filter + var oldPizzaRestaurant = _restaurantsCollection.Find(filter).First(); + var oldId = oldPizzaRestaurant.Id; + // Generates a new restaurant document Restaurant newPizzaRestaurant = new() { + Id = oldId Name = "Mongo's Pizza", Cuisine = "Pizza", Address = new Address()