IMongoDatabaseExtensions.cs 4.6 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 IMongoDatabase.
  23. /// </summary>
  24. public static class IMongoDatabaseExtensions
  25. {
  26. /// <summary>
  27. /// Watches changes on all collection in a database.
  28. /// </summary>
  29. /// <param name="database">The database.</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 IMongoDatabase database,
  37. ChangeStreamOptions options = null,
  38. CancellationToken cancellationToken = default(CancellationToken))
  39. {
  40. Ensure.IsNotNull(database, nameof(database));
  41. var emptyPipeline = new EmptyPipelineDefinition<ChangeStreamDocument<BsonDocument>>();
  42. return database.Watch(emptyPipeline, options, cancellationToken);
  43. }
  44. /// <summary>
  45. /// Watches changes on all collection in a database.
  46. /// </summary>
  47. /// <param name="database">The database.</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 IMongoDatabase database,
  56. IClientSessionHandle session,
  57. ChangeStreamOptions options = null,
  58. CancellationToken cancellationToken = default(CancellationToken))
  59. {
  60. Ensure.IsNotNull(database, nameof(database));
  61. Ensure.IsNotNull(session, nameof(session));
  62. var emptyPipeline = new EmptyPipelineDefinition<ChangeStreamDocument<BsonDocument>>();
  63. return database.Watch(session, emptyPipeline, options, cancellationToken);
  64. }
  65. /// <summary>
  66. /// Watches changes on all collection in a database.
  67. /// </summary>
  68. /// <param name="database">The database.</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 IMongoDatabase database,
  76. ChangeStreamOptions options = null,
  77. CancellationToken cancellationToken = default(CancellationToken))
  78. {
  79. Ensure.IsNotNull(database, nameof(database));
  80. var emptyPipeline = new EmptyPipelineDefinition<ChangeStreamDocument<BsonDocument>>();
  81. return database.WatchAsync(emptyPipeline, options, cancellationToken);
  82. }
  83. /// <summary>
  84. /// Watches changes on all collection in a database.
  85. /// </summary>
  86. /// <param name="database">The database.</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 IMongoDatabase database,
  95. IClientSessionHandle session,
  96. ChangeStreamOptions options = null,
  97. CancellationToken cancellationToken = default(CancellationToken))
  98. {
  99. Ensure.IsNotNull(database, nameof(database));
  100. Ensure.IsNotNull(session, nameof(session));
  101. var emptyPipeline = new EmptyPipelineDefinition<ChangeStreamDocument<BsonDocument>>();
  102. return database.WatchAsync(session, emptyPipeline, options, cancellationToken);
  103. }
  104. }
  105. }