ReplaceOneModel.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* Copyright 2010-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using MongoDB.Driver.Core.Misc;
  21. using MongoDB.Driver.Core.Operations;
  22. namespace MongoDB.Driver
  23. {
  24. /// <summary>
  25. /// Model for replacing a single document.
  26. /// </summary>
  27. /// <typeparam name="TDocument">The type of the document.</typeparam>
  28. #if NET452
  29. [Serializable]
  30. #endif
  31. public sealed class ReplaceOneModel<TDocument> : WriteModel<TDocument>
  32. {
  33. // fields
  34. private Collation _collation;
  35. private readonly FilterDefinition<TDocument> _filter;
  36. private bool _isUpsert;
  37. private readonly TDocument _replacement;
  38. // constructors
  39. /// <summary>
  40. /// Initializes a new instance of the <see cref="ReplaceOneModel{TDocument}"/> class.
  41. /// </summary>
  42. /// <param name="filter">The filter.</param>
  43. /// <param name="replacement">The replacement.</param>
  44. public ReplaceOneModel(FilterDefinition<TDocument> filter, TDocument replacement)
  45. {
  46. _filter = Ensure.IsNotNull(filter, nameof(filter));
  47. _replacement = replacement;
  48. }
  49. // properties
  50. /// <summary>
  51. /// Gets or sets the collation.
  52. /// </summary>
  53. public Collation Collation
  54. {
  55. get { return _collation; }
  56. set { _collation = value; }
  57. }
  58. /// <summary>
  59. /// Gets the filter.
  60. /// </summary>
  61. public FilterDefinition<TDocument> Filter
  62. {
  63. get { return _filter; }
  64. }
  65. /// <summary>
  66. /// Gets or sets a value indicating whether to insert the document if it doesn't already exist.
  67. /// </summary>
  68. public bool IsUpsert
  69. {
  70. get { return _isUpsert; }
  71. set { _isUpsert = value; }
  72. }
  73. /// <summary>
  74. /// Gets the replacement.
  75. /// </summary>
  76. public TDocument Replacement
  77. {
  78. get { return _replacement; }
  79. }
  80. /// <summary>
  81. /// Gets the type of the model.
  82. /// </summary>
  83. public override WriteModelType ModelType
  84. {
  85. get { return WriteModelType.ReplaceOne; }
  86. }
  87. }
  88. }