diff --git a/source/fundamentals/crud/write-operations/update-many.txt b/source/fundamentals/crud/write-operations/update-many.txt index e05203fd..7e219246 100644 --- a/source/fundamentals/crud/write-operations/update-many.txt +++ b/source/fundamentals/crud/write-operations/update-many.txt @@ -36,7 +36,15 @@ Update Many .. replacement:: document-or-documents documents - + + .. replacement:: options-ref + + .. _csharp-update-many-options: + + .. replacement:: options-ref-link + + :ref:`csharp-update-many-options` + .. replacement:: fields-link :ref:`csharp-update-many-fields` diff --git a/source/fundamentals/crud/write-operations/update-many/fields.txt b/source/fundamentals/crud/write-operations/update-many/fields.txt index 09ba5a52..f261fd0b 100644 --- a/source/fundamentals/crud/write-operations/update-many/fields.txt +++ b/source/fundamentals/crud/write-operations/update-many/fields.txt @@ -25,4 +25,204 @@ Update Fields in Many Documents .. replacement:: update-page-link - :ref:`` \ No newline at end of file + :ref:`csharp-update-many` + + .. replacement:: matching-document-or-documents + + all matching documents + + .. replacement:: inc-code-example-tabs + + .. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. literalinclude:: /includes/code-examples/update-many/UpdateManyFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-many-inc + :end-before: end-update-many-inc + + .. tab:: Asynchronous + :tabid: async + + .. literalinclude:: /includes/code-examples/update-many/UpdateManyFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-many-inc-async + :end-before: end-update-many-inc-async + + .. replacement:: mul-code-example-tabs + + .. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. literalinclude:: /includes/code-examples/update-many/UpdateManyFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-many-mul + :end-before: end-update-many-mul + + .. tab:: Asynchronous + :tabid: async + + .. literalinclude:: /includes/code-examples/update-many/UpdateManyFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-many-mul-async + :end-before: end-update-many-mul-async + + .. replacement:: rename-code-example-tabs + + .. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. literalinclude:: /includes/code-examples/update-many/UpdateManyFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-many-rename + :end-before: end-update-many-rename + + .. tab:: Asynchronous + :tabid: async + + .. literalinclude:: /includes/code-examples/update-many/UpdateManyFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-many-rename-async + :end-before: end-update-many-rename-async + + .. replacement:: set-code-example-tabs + + .. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. literalinclude:: /includes/code-examples/update-many/UpdateManyFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-many-set + :end-before: end-update-many-set + + .. tab:: Asynchronous + :tabid: async + + .. literalinclude:: /includes/code-examples/update-many/UpdateManyFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-many-set-async + :end-before: end-update-many-set-async + + .. replacement:: max-code-example-tabs + + .. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. literalinclude:: /includes/code-examples/update-many/UpdateManyFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-many-max + :end-before: end-update-many-max + + .. tab:: Asynchronous + :tabid: async + + .. literalinclude:: /includes/code-examples/update-many/UpdateManyFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-many-max-async + :end-before: end-update-many-max-async + + .. replacement:: setoninsert-code-example-tabs + + .. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. literalinclude:: /includes/code-examples/update-many/UpdateManyFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-many-setoninsert + :end-before: end-update-many-setoninsert + + .. tab:: Asynchronous + :tabid: async + + .. literalinclude:: /includes/code-examples/update-many/UpdateManyFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-many-setoninsert-async + :end-before: end-update-many-setoninsert-async + + .. replacement:: options-ref-link + + :ref:`csharp-update-many-options` in the Update Many guide + + .. replacement:: currentdate-code-example-tabs + + .. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. literalinclude:: /includes/code-examples/update-many/UpdateManyFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-many-currentdate + :end-before: end-update-many-currentdate + + .. tab:: Asynchronous + :tabid: async + + .. literalinclude:: /includes/code-examples/update-many/UpdateManyFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-many-currentdate-async + :end-before: end-update-many-currentdate-async + + .. replacement:: unset-code-example-tabs + + .. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. literalinclude:: /includes/code-examples/update-many/UpdateManyFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-many-unset + :end-before: end-update-many-unset + + .. tab:: Asynchronous + :tabid: async + + .. literalinclude:: /includes/code-examples/update-many/UpdateManyFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-many-unset-async + :end-before: end-update-many-unset-async diff --git a/source/fundamentals/crud/write-operations/update-one.txt b/source/fundamentals/crud/write-operations/update-one.txt index 192bf0ed..5580b2b2 100644 --- a/source/fundamentals/crud/write-operations/update-one.txt +++ b/source/fundamentals/crud/write-operations/update-one.txt @@ -36,7 +36,15 @@ Update One .. replacement:: document-or-documents document - + + .. replacement:: options-ref + + .. _csharp-update-one-options: + + .. replacement:: options-ref-link + + :ref:`csharp-update-one-options` + .. replacement:: fields-link :ref:`csharp-update-one-fields` @@ -111,4 +119,4 @@ Update One :copyable: true :dedent: :start-after: // start-pipeline-async - :end-before: // end-pipeline-async \ No newline at end of file + :end-before: // end-pipeline-async diff --git a/source/fundamentals/crud/write-operations/update-one/fields.txt b/source/fundamentals/crud/write-operations/update-one/fields.txt index 226d68ef..5cba1803 100644 --- a/source/fundamentals/crud/write-operations/update-one/fields.txt +++ b/source/fundamentals/crud/write-operations/update-one/fields.txt @@ -25,4 +25,204 @@ Update Fields in One Document .. replacement:: update-page-link - :ref:`` \ No newline at end of file + :ref:`csharp-update-one` + + .. replacement:: matching-document-or-documents + + the matching document + + .. replacement:: inc-code-example-tabs + + .. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. literalinclude:: /includes/code-examples/update-one/UpdateOneFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-one-inc + :end-before: end-update-one-inc + + .. tab:: Asynchronous + :tabid: async + + .. literalinclude:: /includes/code-examples/update-one/UpdateOneFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-one-inc-async + :end-before: end-update-one-inc-async + + .. replacement:: mul-code-example-tabs + + .. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. literalinclude:: /includes/code-examples/update-one/UpdateOneFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-one-mul + :end-before: end-update-one-mul + + .. tab:: Asynchronous + :tabid: async + + .. literalinclude:: /includes/code-examples/update-one/UpdateOneFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-one-mul-async + :end-before: end-update-one-mul-async + + .. replacement:: rename-code-example-tabs + + .. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. literalinclude:: /includes/code-examples/update-one/UpdateOneFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-one-rename + :end-before: end-update-one-rename + + .. tab:: Asynchronous + :tabid: async + + .. literalinclude:: /includes/code-examples/update-one/UpdateOneFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-one-rename-async + :end-before: end-update-one-rename-async + + .. replacement:: set-code-example-tabs + + .. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. literalinclude:: /includes/code-examples/update-one/UpdateOneFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-one-set + :end-before: end-update-one-set + + .. tab:: Asynchronous + :tabid: async + + .. literalinclude:: /includes/code-examples/update-one/UpdateOneFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-one-set-async + :end-before: end-update-one-set-async + + .. replacement:: max-code-example-tabs + + .. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. literalinclude:: /includes/code-examples/update-one/UpdateOneFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-one-max + :end-before: end-update-one-max + + .. tab:: Asynchronous + :tabid: async + + .. literalinclude:: /includes/code-examples/update-one/UpdateOneFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-one-max-async + :end-before: end-update-one-max-async + + .. replacement:: setoninsert-code-example-tabs + + .. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. literalinclude:: /includes/code-examples/update-one/UpdateOneFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-one-setoninsert + :end-before: end-update-one-setoninsert + + .. tab:: Asynchronous + :tabid: async + + .. literalinclude:: /includes/code-examples/update-one/UpdateOneFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-one-setoninsert-async + :end-before: end-update-one-setoninsert-async + + .. replacement:: options-ref-link + + :ref:`csharp-update-one-options` in the Update One guide + + .. replacement:: currentdate-code-example-tabs + + .. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. literalinclude:: /includes/code-examples/update-one/UpdateOneFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-one-currentdate + :end-before: end-update-one-currentdate + + .. tab:: Asynchronous + :tabid: async + + .. literalinclude:: /includes/code-examples/update-one/UpdateOneFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-one-currentdate-async + :end-before: end-update-one-currentdate-async + + .. replacement:: unset-code-example-tabs + + .. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. literalinclude:: /includes/code-examples/update-one/UpdateOneFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-one-unset + :end-before: end-update-one-unset + + .. tab:: Asynchronous + :tabid: async + + .. literalinclude:: /includes/code-examples/update-one/UpdateOneFields.cs + :language: csharp + :copyable: true + :dedent: + :start-after: start-update-one-unset-async + :end-before: end-update-one-unset-async diff --git a/source/includes/code-examples/update-many/UpdateManyFields.cs b/source/includes/code-examples/update-many/UpdateManyFields.cs new file mode 100644 index 00000000..3c1bd2df --- /dev/null +++ b/source/includes/code-examples/update-many/UpdateManyFields.cs @@ -0,0 +1,172 @@ +using MongoDB.Bson; +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson.Serialization.Conventions; +using MongoDB.Driver; + +public class Program +{ + + public static async Task Main(string[] args) + { + // Replace with your connection string + const string uri = ""; + + // This allows automapping of the camelCase database fields to our models. + var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() }; + ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true); + + var mongoClient = new MongoClient(uri); + var database = mongoClient.GetDatabase("sample_restaurants"); + var _restaurantsCollection = database.GetCollection("restaurants"); + + // start-update-many-inc + var filter = Builders.Filter.Eq("name", "Shake Shack"); + + var update = Builders.Update + .Inc(restaurant => restaurant.Grades[0].Score, 2); + + var result = _restaurantsCollection.UpdateMany(filter, update); + // end-update-many-inc + + // start-update-many-inc-async + var filter = Builders.Filter.Eq("name", "Shake Shack"); + + var update = Builders.Update + .Inc(restaurant => restaurant.Grades[0].Score, 2); + + var result = await _restaurantsCollection.UpdateManyAsync(filter, update); + // end-update-many-inc-async + + // start-update-many-mul + var filter = Builders.Filter.Eq("name", "Shake Shack"); + + var update = Builders.Update + .Mul(restaurant => restaurant.Grades[0].Score, 1.25f); + + var result = _restaurantsCollection.UpdateMany(filter, update); + // end-update-many-mul + + // start-update-many-mul-async + var filter = Builders.Filter.Eq("name", "Shake Shack"); + + var update = Builders.Update + .Mul(restaurant => restaurant.Grades[0].Score, 1.25f); + + var result = await _restaurantsCollection.UpdateManyAsync(filter, update); + // end-update-many-mul-async + + // start-update-many-rename + var filter = Builders.Filter.Eq("name", "Shake Shack"); + + var update = Builders.Update + .Rename(restaurant => restaurant.Address, "location"); + + var result = _restaurantsCollection.UpdateMany(filter, update); + // end-update-many-rename + + // start-update-many-rename-async + var filter = Builders.Filter.Eq("name", "Shake Shack"); + + var update = Builders.Update + .Rename(restaurant => restaurant.Address, "location"); + + var result = await _restaurantsCollection.UpdateManyAsync(filter, update); + // end-update-many-rename-async + + // start-update-many-set + var filter = Builders.Filter.Eq("name", "Shake Shack"); + + var update = Builders.Update + .Set("recommended", true); + + var result = _restaurantsCollection.UpdateMany(filter, update); + // end-update-many-set + + // start-update-many-set-async + var filter = Builders.Filter.Eq("name", "Shake Shack"); + + var update = Builders.Update + .Set("recommended", true); + + var result = await _restaurantsCollection.UpdateManyAsync(filter, update); + // end-update-many-set-async + + // start-update-many-max + var filter = Builders.Filter.Eq("name", "Shake Shack"); + + var update = Builders.Update + .Max(restaurant => restaurant.Grades[0].Score, 20); + + var result = _restaurantsCollection.UpdateMany(filter, update); + // end-update-many-max + + // start-update-many-max-async + var filter = Builders.Filter.Eq("name", "Shake Shack"); + + var update = Builders.Update + .Max(restaurant => restaurant.Grades[0].Score, 20); + + var result = await _restaurantsCollection.UpdateManyAsync(filter, update); + // end-update-many-max-async + + // start-update-many-setoninsert + var filter = Builders.Filter.Eq("name", "Patty's Pies"); + + var update = Builders.Update + .SetOnInsert("recommended", true); + + var result = _restaurantsCollection.UpdateMany( + filter, update, new UpdateOptions { IsUpsert = true } + ); + // end-update-many-setoninsert + + // start-update-many-setoninsert-async + var filter = Builders.Filter.Eq("name", "Patty's Pies"); + + var update = Builders.Update + .SetOnInsert("recommended", true); + + var result = await _restaurantsCollection.UpdateManyAsync( + filter, update, new UpdateOptions { IsUpsert = true } + ); + // end-update-many-setoninsert-async + + // start-update-many-currentdate + var filter = Builders.Filter.Eq("name", "Shake Shack"); + + var update = Builders.Update + .CurrentDate(restaurant => restaurant.Grades[0].Date); + + var result = _restaurantsCollection.UpdateMany(filter, update); + // end-update-many-currentdate + + // start-update-many-currentdate-async + var filter = Builders.Filter.Eq("name", "Shake Shack"); + + var update = Builders.Update + .CurrentDate(restaurant => restaurant.Grades[0].Date); + + var result = await _restaurantsCollection.UpdateManyAsync(filter, update); + // end-update-many-currentdate-async + + // start-update-many-unset + var filter = Builders.Filter.Eq("name", "Shake Shack"); + + var update = Builders.Update + .Unset(restaurant => restaurant.Cuisine); + + var result = _restaurantsCollection.UpdateMany(filter, update); + // end-update-many-unset + + // start-update-many-unset-async + var filter = Builders.Filter.Eq("name", "Shake Shack"); + + var update = Builders.Update + .Unset(restaurant => restaurant.Cuisine); + + var result = await _restaurantsCollection.UpdateManyAsync(filter, update); + // end-update-many-unset-async + + } + +} diff --git a/source/includes/code-examples/update-one/UpdateOneFields.cs b/source/includes/code-examples/update-one/UpdateOneFields.cs new file mode 100644 index 00000000..f50a1339 --- /dev/null +++ b/source/includes/code-examples/update-one/UpdateOneFields.cs @@ -0,0 +1,172 @@ +using MongoDB.Bson; +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson.Serialization.Conventions; +using MongoDB.Driver; + +public class Program +{ + + public static async Task Main(string[] args) + { + // Replace with your connection string + const string uri = ""; + + // This allows automapping of the camelCase database fields to our models. + var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() }; + ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true); + + var mongoClient = new MongoClient(uri); + var database = mongoClient.GetDatabase("sample_restaurants"); + var _restaurantsCollection = database.GetCollection("restaurants"); + + // start-update-one-inc + var filter = Builders.Filter.Eq("name", "Casa Bella"); + + var update = Builders.Update + .Inc(restaurant => restaurant.Grades[0].Score, 2); + + var result = _restaurantsCollection.UpdateOne(filter, update); + // end-update-one-inc + + // start-update-one-inc-async + var filter = Builders.Filter.Eq("name", "Casa Bella"); + + var update = Builders.Update + .Inc(restaurant => restaurant.Grades[0].Score, 2); + + var result = await _restaurantsCollection.UpdateOneAsync(filter, update); + // end-update-one-inc-async + + // start-update-one-mul + var filter = Builders.Filter.Eq("name", "Casa Bella"); + + var update = Builders.Update + .Mul(restaurant => restaurant.Grades[0].Score, 1.25f); + + var result = _restaurantsCollection.UpdateOne(filter, update); + // end-update-one-mul + + // start-update-one-mul-async + var filter = Builders.Filter.Eq("name", "Casa Bella"); + + var update = Builders.Update + .Mul(restaurant => restaurant.Grades[0].Score, 1.25f); + + var result = await _restaurantsCollection.UpdateOneAsync(filter, update); + // end-update-one-mul-async + + // start-update-one-rename + var filter = Builders.Filter.Eq("name", "Casa Bella"); + + var update = Builders.Update + .Rename(restaurant => restaurant.Address, "location"); + + var result = _restaurantsCollection.UpdateOne(filter, update); + // end-update-one-rename + + // start-update-one-rename-async + var filter = Builders.Filter.Eq("name", "Casa Bella"); + + var update = Builders.Update + .Rename(restaurant => restaurant.Address, "location"); + + var result = await _restaurantsCollection.UpdateOneAsync(filter, update); + // end-update-one-rename-async + + // start-update-one-set + var filter = Builders.Filter.Eq("name", "Casa Bella"); + + var update = Builders.Update + .Set("recommended", true); + + var result = _restaurantsCollection.UpdateOne(filter, update); + // end-update-one-set + + // start-update-one-set-async + var filter = Builders.Filter.Eq("name", "Casa Bella"); + + var update = Builders.Update + .Set("recommended", true); + + var result = await _restaurantsCollection.UpdateOneAsync(filter, update); + // end-update-one-set-async + + // start-update-one-max + var filter = Builders.Filter.Eq("name", "Casa Bella"); + + var update = Builders.Update + .Max(restaurant => restaurant.Grades[0].Score, 20); + + var result = _restaurantsCollection.UpdateOne(filter, update); + // end-update-one-max + + // start-update-one-max-async + var filter = Builders.Filter.Eq("name", "Casa Bella"); + + var update = Builders.Update + .Max(restaurant => restaurant.Grades[0].Score, 20); + + var result = await _restaurantsCollection.UpdateOneAsync(filter, update); + // end-update-one-max-async + + // start-update-one-setoninsert + var filter = Builders.Filter.Eq("name", "Patty's Pies"); + + var update = Builders.Update + .SetOnInsert("recommended", true); + + var result = _restaurantsCollection.UpdateOne( + filter, update, new UpdateOptions { IsUpsert = true } + ); + // end-update-one-setoninsert + + // start-update-one-setoninsert-async + var filter = Builders.Filter.Eq("name", "Patty's Pies"); + + var update = Builders.Update + .SetOnInsert("recommended", true); + + var result = await _restaurantsCollection.UpdateOneAsync( + filter, update, new UpdateOptions { IsUpsert = true } + ); + // end-update-one-setoninsert-async + + // start-update-one-currentdate + var filter = Builders.Filter.Eq("name", "Casa Bella"); + + var update = Builders.Update + .CurrentDate(restaurant => restaurant.Grades[0].Date); + + var result = _restaurantsCollection.UpdateOne(filter, update); + // end-update-one-currentdate + + // start-update-one-currentdate-async + var filter = Builders.Filter.Eq("name", "Casa Bella"); + + var update = Builders.Update + .CurrentDate(restaurant => restaurant.Grades[0].Date); + + var result = await _restaurantsCollection.UpdateOneAsync(filter, update); + // end-update-one-currentdate-async + + // start-update-one-unset + var filter = Builders.Filter.Eq("name", "Casa Bella"); + + var update = Builders.Update + .Unset(restaurant => restaurant.Cuisine); + + var result = _restaurantsCollection.UpdateOne(filter, update); + // end-update-one-unset + + // start-update-one-unset-async + var filter = Builders.Filter.Eq("name", "Casa Bella"); + + var update = Builders.Update + .Unset(restaurant => restaurant.Cuisine); + + var result = await _restaurantsCollection.UpdateOneAsync(filter, update); + // end-update-one-unset-async + + } + +} diff --git a/source/includes/page-templates/update/fields.rst b/source/includes/page-templates/update/fields.rst index 287228ca..9109797b 100644 --- a/source/includes/page-templates/update/fields.rst +++ b/source/includes/page-templates/update/fields.rst @@ -39,6 +39,12 @@ method. This method accepts the following parameters: **Data Type:** ``TField`` +The following code example uses the ``Inc()`` method to increment the +``Score`` value by ``2`` in the first ``GradeEntry`` object of the ``Grades`` +array in |matching-document-or-documents|: + +|inc-code-example-tabs| + Multiply a Value ---------------- @@ -62,6 +68,12 @@ method. This method accepts the following parameters: **Data Type:** ``TField`` +The following code example uses the ``Mul()`` method to multiply the +``Score`` value by ``1.25`` in the first ``GradeEntry`` object of the ``Grades`` +array in |matching-document-or-documents|: + +|mul-code-example-tabs| + Rename a Field -------------- @@ -85,6 +97,17 @@ the following parameters: **Data Type:** ``string`` +The following code example uses the ``Rename()`` method to rename the +``Address`` field to ``location`` in |matching-document-or-documents|: + +|rename-code-example-tabs| + +.. note:: + + The preceding code examples use the field name ``location`` because + this matches the naming scheme of the field names in MongoDB, + not the property names in the ``Restaurant`` class. + Set a Value ----------- @@ -108,6 +131,17 @@ method. This method accepts the following parameters: **Data Type:** ``TField`` +The following code example uses the ``Set()`` method to rename the +``recommended`` field to ``true`` in |matching-document-or-documents|: + +|set-code-example-tabs| + +.. note:: + + The preceding code examples use the field name ``recommended`` because + this matches the naming scheme of the field names in MongoDB, + not the property names in the ``Restaurant`` class. + Set by Comparison ----------------- @@ -153,6 +187,13 @@ method. This method accepts the following parameters: **Data Type:** ``TField`` +The following code example uses the ``Max()`` method to set the +``Score`` field of the first ``GradeEntry`` object to +the greater of ``20`` and the current value in +|matching-document-or-documents|: + +|max-code-example-tabs| + Set on Insert ------------- @@ -176,6 +217,20 @@ To set the value of a field only if the document was upserted by the same operat **Data Type:** ``TField`` +The following code example uses the ``SetOnInsert()`` method to set the +``recommended`` field to ``true`` in |matching-document-or-documents|: + +|setoninsert-code-example-tabs| + +.. note:: + + The preceding code examples use the field name ``recommended`` because + this matches the naming scheme of the field names in MongoDB, + not the property names in the ``Restaurant`` class. + +To learn more about upserting documents, see +|options-ref-link|. + Set the Current Date -------------------- @@ -200,6 +255,12 @@ To set the value of a field to the current date and time, call the **Data Type:** `UpdateDefinitionCurrentDateType? <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.UpdateDefinitionCurrentDateType.html>`__ +The following code example uses the ``CurrentDate()`` method to update the +``Date`` field of the first ``GradeEntry`` object to the current date in +|matching-document-or-documents|: + +|currentdate-code-example-tabs| + Unset a Field ------------- @@ -218,6 +279,11 @@ method accepts the following parameter: **Data Type:** ``Expression>`` +The following code example uses the ``Unset()`` method to remove the +``Cuisine`` field in |matching-document-or-documents|: + +|unset-code-example-tabs| + API Documentation ----------------- @@ -232,4 +298,4 @@ guide, see the following API documentation: - `Builders.Update.Min() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.UpdateDefinitionBuilder-1.Min.html>`__ - `Builders.Update.SetOnInsert() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.UpdateDefinitionBuilder-1.SetOnInsert.html>`__ - `Builders.Update.CurrentDate() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.UpdateDefinitionBuilder-1.CurrentDate.html>`__ -- `Builders.Update.Unset() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.UpdateDefinitionBuilder-1.Unset.html>`__ \ No newline at end of file +- `Builders.Update.Unset() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.UpdateDefinitionBuilder-1.Unset.html>`__ diff --git a/source/includes/page-templates/update/update.rst b/source/includes/page-templates/update/update.rst index 49741885..f55165d2 100644 --- a/source/includes/page-templates/update/update.rst +++ b/source/includes/page-templates/update/update.rst @@ -44,7 +44,7 @@ The |sync-method| and |async-method| methods accept the following parameters: * - ``options`` - *Optional.* An instance of the ``UpdateOptions`` class that specifies the configuration for the update operation. The default value is ``null``. For a list - of available options, see :ref:`csharp-update-options`. + of available options, see |options-ref-link|. **Data Type:** `UpdateOptions <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.UpdateOptions.html>`__ @@ -130,7 +130,7 @@ operation: :manual:`Updates with Aggregation Pipeline ` in the {+mdb-server+} manual. -.. _csharp-update-options: +|options-ref| Configuration Options --------------------- @@ -261,4 +261,4 @@ guide, see the following API documentation: - `UpdateOptions <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.UpdateOptions.html>`__ - `UpdateResult <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.UpdateResult.html>`__ -|instruqt-lab-component| \ No newline at end of file +|instruqt-lab-component|