From 7448f4fd26aa6840e6fc95cdd3f3ea25833335e0 Mon Sep 17 00:00:00 2001 From: Stephanie Aurelio <stephanie.aurelio@mongodb.com> Date: Tue, 7 Jan 2025 15:15:16 -0800 Subject: [PATCH 1/7] update callout and examples --- source/fundamentals/crud/write-operations/replace.txt | 7 +++++-- source/includes/code-examples/replace-one/ReplaceOne.cs | 2 ++ .../includes/code-examples/replace-one/ReplaceOneAsync.cs | 2 ++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/source/fundamentals/crud/write-operations/replace.txt b/source/fundamentals/crud/write-operations/replace.txt index e64af0d9..d422af5a 100644 --- a/source/fundamentals/crud/write-operations/replace.txt +++ b/source/fundamentals/crud/write-operations/replace.txt @@ -109,8 +109,11 @@ 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 must add the ``[BsonIgnoreIfDefault]`` attribute + to the ``_id`` field in your Plain Old CLR/Class Object (POCO). 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 4f37ae43..bb531d6f 100644 --- a/source/includes/code-examples/replace-one/ReplaceOne.cs +++ b/source/includes/code-examples/replace-one/ReplaceOne.cs @@ -115,6 +115,8 @@ private static void Setup() public class Restaurant { + [BsonId] + [BsonIgnoreIfDefault] public ObjectId Id { get; set; } public string Name { get; set; } diff --git a/source/includes/code-examples/replace-one/ReplaceOneAsync.cs b/source/includes/code-examples/replace-one/ReplaceOneAsync.cs index e3e79743..dcfbe2a5 100644 --- a/source/includes/code-examples/replace-one/ReplaceOneAsync.cs +++ b/source/includes/code-examples/replace-one/ReplaceOneAsync.cs @@ -114,6 +114,8 @@ private static void Setup() public class Restaurant { + [BsonId] + [BsonIgnoreIfDefault] public ObjectId Id { get; set; } public string Name { get; set; } From ac21254f686480fa15d2c72924d88424ebbea636 Mon Sep 17 00:00:00 2001 From: Stephanie Aurelio <stephanie.aurelio@mongodb.com> Date: Tue, 7 Jan 2025 15:27:16 -0800 Subject: [PATCH 2/7] update callout --- .../crud/write-operations/replace.txt | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/source/fundamentals/crud/write-operations/replace.txt b/source/fundamentals/crud/write-operations/replace.txt index d422af5a..435d1b51 100644 --- a/source/fundamentals/crud/write-operations/replace.txt +++ b/source/fundamentals/crud/write-operations/replace.txt @@ -111,9 +111,33 @@ corresponding code. 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 + the existing document. + + If your replacement document does not specify a value for the ``_id`` field, you must add the ``[BsonIgnoreIfDefault]`` attribute - to the ``_id`` field in your Plain Old CLR/Class Object (POCO). + to the ``_id`` field in your Plain Old CLR/Class Object (POCO) like the following: + + .. code-block:: csharp + + public class Restaurant + { + [BsonId] + [BsonIgnoreIfDefault] + public ObjectId Id { get; set; } + + public string Name { get; set; } + + [BsonElement("restaurant_id")] + public string RestaurantId { get; set; } + + public string Cuisine { get; set; } + + public Address Address { get; set; } + + public string Borough { get: set; } + + public List<GradeEntry> Grades { get; set; } + } Customize the Replace Operation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From e368483ae8050f792f588192ea93710ba3e7aa2e Mon Sep 17 00:00:00 2001 From: Stephanie Aurelio <stephanie.aurelio@mongodb.com> Date: Tue, 7 Jan 2025 16:08:39 -0800 Subject: [PATCH 3/7] edits --- .../crud/write-operations/replace.txt | 24 +++++++------------ .../code-examples/replace-one/ReplaceOne.cs | 1 + .../replace-one/ReplaceOneAsync.cs | 1 + 3 files changed, 10 insertions(+), 16 deletions(-) diff --git a/source/fundamentals/crud/write-operations/replace.txt b/source/fundamentals/crud/write-operations/replace.txt index 435d1b51..87d12846 100644 --- a/source/fundamentals/crud/write-operations/replace.txt +++ b/source/fundamentals/crud/write-operations/replace.txt @@ -113,30 +113,22 @@ corresponding code. 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 must add the ``[BsonIgnoreIfDefault]`` attribute - to the ``_id`` field in your Plain Old CLR/Class Object (POCO) like the following: + If your replacement document does not specify a value for the ``_id`` field, + you must add the ``[BsonIgnoreIfDefault]`` attribute to the ``_id`` field in + your Plain Old CLR/Class Object (POCO). This prevents the {+driver-short+} + from serializing the default value of the ``_id``. The following example + shows how to add this attribute: .. code-block:: csharp public class Restaurant { [BsonId] + [BsonRepresentation(BsonType.ObjectId)] [BsonIgnoreIfDefault] - public ObjectId Id { get; set; } + public string Id { get; set; } - public string Name { get; set; } - - [BsonElement("restaurant_id")] - public string RestaurantId { get; set; } - - public string Cuisine { get; set; } - - public Address Address { get; set; } - - public string Borough { get: set; } - - public List<GradeEntry> Grades { 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 bb531d6f..53e58967 100644 --- a/source/includes/code-examples/replace-one/ReplaceOne.cs +++ b/source/includes/code-examples/replace-one/ReplaceOne.cs @@ -116,6 +116,7 @@ private static void Setup() public class Restaurant { [BsonId] + [BsonRepresentation(BsonType.ObjectId)] [BsonIgnoreIfDefault] public ObjectId Id { get; set; } diff --git a/source/includes/code-examples/replace-one/ReplaceOneAsync.cs b/source/includes/code-examples/replace-one/ReplaceOneAsync.cs index dcfbe2a5..c7369670 100644 --- a/source/includes/code-examples/replace-one/ReplaceOneAsync.cs +++ b/source/includes/code-examples/replace-one/ReplaceOneAsync.cs @@ -115,6 +115,7 @@ private static void Setup() public class Restaurant { [BsonId] + [BsonRepresentation(BsonType.ObjectId)] [BsonIgnoreIfDefault] public ObjectId Id { get; set; } From 7bcec6e92f72dfaa5e4fab97789cc802055a3e10 Mon Sep 17 00:00:00 2001 From: Stephanie Aurelio <stephanie.aurelio@mongodb.com> Date: Mon, 13 Jan 2025 11:27:09 -0800 Subject: [PATCH 4/7] update code examples and RM feedback --- .../fundamentals/crud/write-operations/replace.txt | 9 +++------ .../code-examples/replace-one/ReplaceOne.cs | 13 ++++++++++--- .../code-examples/replace-one/ReplaceOneAsync.cs | 14 +++++++++++--- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/source/fundamentals/crud/write-operations/replace.txt b/source/fundamentals/crud/write-operations/replace.txt index 87d12846..bb68ffb1 100644 --- a/source/fundamentals/crud/write-operations/replace.txt +++ b/source/fundamentals/crud/write-operations/replace.txt @@ -115,18 +115,15 @@ corresponding code. If your replacement document does not specify a value for the ``_id`` field, you must add the ``[BsonIgnoreIfDefault]`` attribute to the ``_id`` field in - your Plain Old CLR/Class Object (POCO). This prevents the {+driver-short+} - from serializing the default value of the ``_id``. The following example - shows how to add this attribute: + your Plain Old CLR/Class Object (POCO). The following example shows how to + add this attribute: .. code-block:: csharp public class Restaurant { - [BsonId] - [BsonRepresentation(BsonType.ObjectId)] [BsonIgnoreIfDefault] - public string Id { get; set; } + public ObjectId Id { get; set; } // Other properties } diff --git a/source/includes/code-examples/replace-one/ReplaceOne.cs b/source/includes/code-examples/replace-one/ReplaceOne.cs index 53e58967..7cd7efa9 100644 --- a/source/includes/code-examples/replace-one/ReplaceOne.cs +++ b/source/includes/code-examples/replace-one/ReplaceOne.cs @@ -52,9 +52,14 @@ private static ReplaceOneResult ReplaceOneRestaurant() var filter = Builders<Restaurant>.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() @@ -77,9 +82,14 @@ private static ReplaceOneResult ReplaceOneRestaurantWithOptions() var filter = Builders<Restaurant>.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() @@ -115,9 +125,6 @@ private static void Setup() public class Restaurant { - [BsonId] - [BsonRepresentation(BsonType.ObjectId)] - [BsonIgnoreIfDefault] public ObjectId Id { get; set; } public string Name { get; set; } diff --git a/source/includes/code-examples/replace-one/ReplaceOneAsync.cs b/source/includes/code-examples/replace-one/ReplaceOneAsync.cs index c7369670..07d5b8ef 100644 --- a/source/includes/code-examples/replace-one/ReplaceOneAsync.cs +++ b/source/includes/code-examples/replace-one/ReplaceOneAsync.cs @@ -51,10 +51,15 @@ private static async Task<ReplaceOneResult> ReplaceOneRestaurantAsync() // Creates a filter for all restaurant documents that have a "cuisine" value of "Pizza" var filter = Builders<Restaurant>.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() @@ -76,9 +81,14 @@ private static async Task<ReplaceOneResult> ReplaceOneRestaurantAsyncWithOptions var filter = Builders<Restaurant>.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() @@ -114,8 +124,6 @@ private static void Setup() public class Restaurant { - [BsonId] - [BsonRepresentation(BsonType.ObjectId)] [BsonIgnoreIfDefault] public ObjectId Id { get; set; } From db7bc9ef2b257ff83361f4703a64a6c81a1a1ef7 Mon Sep 17 00:00:00 2001 From: Stephanie Aurelio <stephanie.aurelio@mongodb.com> Date: Mon, 13 Jan 2025 11:36:07 -0800 Subject: [PATCH 5/7] update example --- source/includes/code-examples/replace-one/ReplaceOneAsync.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/source/includes/code-examples/replace-one/ReplaceOneAsync.cs b/source/includes/code-examples/replace-one/ReplaceOneAsync.cs index 07d5b8ef..f8bc1a00 100644 --- a/source/includes/code-examples/replace-one/ReplaceOneAsync.cs +++ b/source/includes/code-examples/replace-one/ReplaceOneAsync.cs @@ -124,7 +124,6 @@ private static void Setup() public class Restaurant { - [BsonIgnoreIfDefault] public ObjectId Id { get; set; } public string Name { get; set; } From 225e18c78492c909b23750fba00e1a1f9537d719 Mon Sep 17 00:00:00 2001 From: Stephanie Aurelio <stephanie.aurelio@mongodb.com> Date: Wed, 15 Jan 2025 11:18:14 -0800 Subject: [PATCH 6/7] tech review feedback --- source/fundamentals/crud/write-operations/replace.txt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/source/fundamentals/crud/write-operations/replace.txt b/source/fundamentals/crud/write-operations/replace.txt index bb68ffb1..090d11c3 100644 --- a/source/fundamentals/crud/write-operations/replace.txt +++ b/source/fundamentals/crud/write-operations/replace.txt @@ -114,9 +114,11 @@ corresponding code. the existing document. If your replacement document does not specify a value for the ``_id`` field, - you must add the ``[BsonIgnoreIfDefault]`` attribute to the ``_id`` field in - your Plain Old CLR/Class Object (POCO). The following example shows how to - add this attribute: + you can add the ``[BsonIgnoreIfDefault]`` attribute to the ``_id`` field in + your Plain Old CLR/Class Object (POCO). Use ``[BsonIgnoreIfDefault]`` if the + ``_id`` field is the ``ObjectId`` type. + + The following example shows how to add this attribute: .. code-block:: csharp From 7e399ca9602594cb6b83744a60e375823cc08ab7 Mon Sep 17 00:00:00 2001 From: Stephanie Aurelio <stephanie.aurelio@mongodb.com> Date: Wed, 15 Jan 2025 11:22:14 -0800 Subject: [PATCH 7/7] edit --- source/fundamentals/crud/write-operations/replace.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/fundamentals/crud/write-operations/replace.txt b/source/fundamentals/crud/write-operations/replace.txt index 090d11c3..34ef27e0 100644 --- a/source/fundamentals/crud/write-operations/replace.txt +++ b/source/fundamentals/crud/write-operations/replace.txt @@ -116,7 +116,7 @@ corresponding code. 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 is the ``ObjectId`` type. + ``_id`` field in your POCO is of the ``ObjectId`` type. The following example shows how to add this attribute: