IMongoClientExtensions.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Copyright 2018-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.Threading;
  16. using System.Threading.Tasks;
  17. using MongoDB.Bson;
  18. using MongoDB.Driver.Core.Misc;
  19. namespace MongoDB.Driver
  20. {
  21. /// <summary>
  22. /// Extension methods on IMongoClient.
  23. /// </summary>
  24. public static class IMongoClientExtensions
  25. {
  26. /// <summary>
  27. /// Watches changes on all collections in all databases.
  28. /// </summary>
  29. /// <param name="client">The client.</param>
  30. /// <param name="options">The options.</param>
  31. /// <param name="cancellationToken">The cancellation token.</param>
  32. /// <returns>
  33. /// A change stream.
  34. /// </returns>
  35. public static IAsyncCursor<ChangeStreamDocument<BsonDocument>> Watch(
  36. this IMongoClient client,
  37. ChangeStreamOptions options = null,
  38. CancellationToken cancellationToken = default(CancellationToken))
  39. {
  40. Ensure.IsNotNull(client, nameof(client));
  41. var emptyPipeline = new EmptyPipelineDefinition<ChangeStreamDocument<BsonDocument>>();
  42. return client.Watch(emptyPipeline, options, cancellationToken);
  43. }
  44. /// <summary>
  45. /// Watches changes on all collections in all databases.
  46. /// </summary>
  47. /// <param name="client">The client.</param>
  48. /// <param name="session">The session.</param>
  49. /// <param name="options">The options.</param>
  50. /// <param name="cancellationToken">The cancellation token.</param>
  51. /// <returns>
  52. /// A change stream.
  53. /// </returns>
  54. public static IAsyncCursor<ChangeStreamDocument<BsonDocument>> Watch(
  55. this IMongoClient client,
  56. IClientSessionHandle session,
  57. ChangeStreamOptions options = null,
  58. CancellationToken cancellationToken = default(CancellationToken))
  59. {
  60. Ensure.IsNotNull(client, nameof(client));
  61. Ensure.IsNotNull(session, nameof(session));
  62. var emptyPipeline = new EmptyPipelineDefinition<ChangeStreamDocument<BsonDocument>>();
  63. return client.Watch(session, emptyPipeline, options, cancellationToken);
  64. }
  65. /// <summary>
  66. /// Watches changes on all collections in all databases.
  67. /// </summary>
  68. /// <param name="client">The client.</param>
  69. /// <param name="options">The options.</param>
  70. /// <param name="cancellationToken">The cancellation token.</param>
  71. /// <returns>
  72. /// A change stream.
  73. /// </returns>
  74. public static Task<IAsyncCursor<ChangeStreamDocument<BsonDocument>>> WatchAsync(
  75. this IMongoClient client,
  76. ChangeStreamOptions options = null,
  77. CancellationToken cancellationToken = default(CancellationToken))
  78. {
  79. Ensure.IsNotNull(client, nameof(client));
  80. var emptyPipeline = new EmptyPipelineDefinition<ChangeStreamDocument<BsonDocument>>();
  81. return client.WatchAsync(emptyPipeline, options, cancellationToken);
  82. }
  83. /// <summary>
  84. /// Watches changes on all collections in all databases.
  85. /// </summary>
  86. /// <param name="client">The client.</param>
  87. /// <param name="session">The session.</param>
  88. /// <param name="options">The options.</param>
  89. /// <param name="cancellationToken">The cancellation token.</param>
  90. /// <returns>
  91. /// A change stream.
  92. /// </returns>
  93. public static Task<IAsyncCursor<ChangeStreamDocument<BsonDocument>>> WatchAsync(
  94. this IMongoClient client,
  95. IClientSessionHandle session,
  96. ChangeStreamOptions options = null,
  97. CancellationToken cancellationToken = default(CancellationToken))
  98. {
  99. Ensure.IsNotNull(client, nameof(client));
  100. Ensure.IsNotNull(session, nameof(session));
  101. var emptyPipeline = new EmptyPipelineDefinition<ChangeStreamDocument<BsonDocument>>();
  102. return client.WatchAsync(session, emptyPipeline, options, cancellationToken);
  103. }
  104. }
  105. }