Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport v2.24] DOCSP-46303 Update replace documents #423

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions source/fundamentals/crud/write-operations/replace.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
10 changes: 10 additions & 0 deletions source/includes/code-examples/replace-one/ReplaceOne.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
12 changes: 11 additions & 1 deletion source/includes/code-examples/replace-one/ReplaceOneAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down