IMongoDatabase.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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.Threading;
  18. using System.Threading.Tasks;
  19. using MongoDB.Bson;
  20. namespace MongoDB.Driver
  21. {
  22. /// <summary>
  23. /// Represents a database in MongoDB.
  24. /// </summary>
  25. /// <remarks>
  26. /// This interface is not guaranteed to remain stable. Implementors should use
  27. /// <see cref="MongoDatabaseBase" />.
  28. /// </remarks>
  29. public interface IMongoDatabase
  30. {
  31. /// <summary>
  32. /// Gets the client.
  33. /// </summary>
  34. IMongoClient Client { get; }
  35. /// <summary>
  36. /// Gets the namespace of the database.
  37. /// </summary>
  38. DatabaseNamespace DatabaseNamespace { get; }
  39. /// <summary>
  40. /// Gets the settings.
  41. /// </summary>
  42. MongoDatabaseSettings Settings { get; }
  43. /// <summary>
  44. /// Creates the collection with the specified name.
  45. /// </summary>
  46. /// <param name="name">The name.</param>
  47. /// <param name="options">The options.</param>
  48. /// <param name="cancellationToken">The cancellation token.</param>
  49. void CreateCollection(string name, CreateCollectionOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  50. /// <summary>
  51. /// Creates the collection with the specified name.
  52. /// </summary>
  53. /// <param name="session">The session.</param>
  54. /// <param name="name">The name.</param>
  55. /// <param name="options">The options.</param>
  56. /// <param name="cancellationToken">The cancellation token.</param>
  57. void CreateCollection(IClientSessionHandle session, string name, CreateCollectionOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  58. /// <summary>
  59. /// Creates the collection with the specified name.
  60. /// </summary>
  61. /// <param name="name">The name.</param>
  62. /// <param name="options">The options.</param>
  63. /// <param name="cancellationToken">The cancellation token.</param>
  64. /// <returns>A task.</returns>
  65. Task CreateCollectionAsync(string name, CreateCollectionOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  66. /// <summary>
  67. /// Creates the collection with the specified name.
  68. /// </summary>
  69. /// <param name="session">The session.</param>
  70. /// <param name="name">The name.</param>
  71. /// <param name="options">The options.</param>
  72. /// <param name="cancellationToken">The cancellation token.</param>
  73. /// <returns>
  74. /// A task.
  75. /// </returns>
  76. Task CreateCollectionAsync(IClientSessionHandle session, string name, CreateCollectionOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  77. /// <summary>
  78. /// Creates a view.
  79. /// </summary>
  80. /// <typeparam name="TDocument">The type of the input documents.</typeparam>
  81. /// <typeparam name="TResult">The type of the pipeline result documents.</typeparam>
  82. /// <param name="viewName">The name of the view.</param>
  83. /// <param name="viewOn">The name of the collection that the view is on.</param>
  84. /// <param name="pipeline">The pipeline.</param>
  85. /// <param name="options">The options.</param>
  86. /// <param name="cancellationToken">The cancellation token.</param>
  87. void CreateView<TDocument, TResult>(string viewName, string viewOn, PipelineDefinition<TDocument, TResult> pipeline, CreateViewOptions<TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken));
  88. /// <summary>
  89. /// Creates a view.
  90. /// </summary>
  91. /// <typeparam name="TDocument">The type of the input documents.</typeparam>
  92. /// <typeparam name="TResult">The type of the pipeline result documents.</typeparam>
  93. /// <param name="session">The session.</param>
  94. /// <param name="viewName">The name of the view.</param>
  95. /// <param name="viewOn">The name of the collection that the view is on.</param>
  96. /// <param name="pipeline">The pipeline.</param>
  97. /// <param name="options">The options.</param>
  98. /// <param name="cancellationToken">The cancellation token.</param>
  99. void CreateView<TDocument, TResult>(IClientSessionHandle session, string viewName, string viewOn, PipelineDefinition<TDocument, TResult> pipeline, CreateViewOptions<TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken));
  100. /// <summary>
  101. /// Creates a view.
  102. /// </summary>
  103. /// <typeparam name="TDocument">The type of the input documents.</typeparam>
  104. /// <typeparam name="TResult">The type of the pipeline result documents.</typeparam>
  105. /// <param name="viewName">The name of the view.</param>
  106. /// <param name="viewOn">The name of the collection that the view is on.</param>
  107. /// <param name="pipeline">The pipeline.</param>
  108. /// <param name="options">The options.</param>
  109. /// <param name="cancellationToken">The cancellation token.</param>
  110. /// <returns>A task.</returns>
  111. Task CreateViewAsync<TDocument, TResult>(string viewName, string viewOn, PipelineDefinition<TDocument, TResult> pipeline, CreateViewOptions<TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken));
  112. /// <summary>
  113. /// Creates a view.
  114. /// </summary>
  115. /// <typeparam name="TDocument">The type of the input documents.</typeparam>
  116. /// <typeparam name="TResult">The type of the pipeline result documents.</typeparam>
  117. /// <param name="session">The session.</param>
  118. /// <param name="viewName">The name of the view.</param>
  119. /// <param name="viewOn">The name of the collection that the view is on.</param>
  120. /// <param name="pipeline">The pipeline.</param>
  121. /// <param name="options">The options.</param>
  122. /// <param name="cancellationToken">The cancellation token.</param>
  123. /// <returns>
  124. /// A task.
  125. /// </returns>
  126. Task CreateViewAsync<TDocument, TResult>(IClientSessionHandle session, string viewName, string viewOn, PipelineDefinition<TDocument, TResult> pipeline, CreateViewOptions<TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken));
  127. /// <summary>
  128. /// Drops the collection with the specified name.
  129. /// </summary>
  130. /// <param name="name">The name of the collection to drop.</param>
  131. /// <param name="cancellationToken">The cancellation token.</param>
  132. void DropCollection(string name, CancellationToken cancellationToken = default(CancellationToken));
  133. /// <summary>
  134. /// Drops the collection with the specified name.
  135. /// </summary>
  136. /// <param name="session">The session.</param>
  137. /// <param name="name">The name of the collection to drop.</param>
  138. /// <param name="cancellationToken">The cancellation token.</param>
  139. void DropCollection(IClientSessionHandle session, string name, CancellationToken cancellationToken = default(CancellationToken));
  140. /// <summary>
  141. /// Drops the collection with the specified name.
  142. /// </summary>
  143. /// <param name="name">The name of the collection to drop.</param>
  144. /// <param name="cancellationToken">The cancellation token.</param>
  145. /// <returns>A task.</returns>
  146. Task DropCollectionAsync(string name, CancellationToken cancellationToken = default(CancellationToken));
  147. /// <summary>
  148. /// Drops the collection with the specified name.
  149. /// </summary>
  150. /// <param name="session">The session.</param>
  151. /// <param name="name">The name of the collection to drop.</param>
  152. /// <param name="cancellationToken">The cancellation token.</param>
  153. /// <returns>
  154. /// A task.
  155. /// </returns>
  156. Task DropCollectionAsync(IClientSessionHandle session, string name, CancellationToken cancellationToken = default(CancellationToken));
  157. /// <summary>
  158. /// Gets a collection.
  159. /// </summary>
  160. /// <typeparam name="TDocument">The document type.</typeparam>
  161. /// <param name="name">The name of the collection.</param>
  162. /// <param name="settings">The settings.</param>
  163. /// <returns>An implementation of a collection.</returns>
  164. IMongoCollection<TDocument> GetCollection<TDocument>(string name, MongoCollectionSettings settings = null);
  165. /// <summary>
  166. /// Lists the names of all the collections in the database.
  167. /// </summary>
  168. /// <param name="options">The options.</param>
  169. /// <param name="cancellationToken">The cancellation token.</param>
  170. /// <returns>A cursor.</returns>
  171. IAsyncCursor<string> ListCollectionNames(ListCollectionNamesOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  172. /// <summary>
  173. /// Lists the names of all the collections in the database.
  174. /// </summary>
  175. /// <param name="session">The session.</param>
  176. /// <param name="options">The options.</param>
  177. /// <param name="cancellationToken">The cancellation token.</param>
  178. /// <returns>A cursor.</returns>
  179. IAsyncCursor<string> ListCollectionNames(IClientSessionHandle session, ListCollectionNamesOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  180. /// <summary>
  181. /// Lists the names of all the collections in the database.
  182. /// </summary>
  183. /// <param name="options">The options.</param>
  184. /// <param name="cancellationToken">The cancellation token.</param>
  185. /// <returns>A Task whose result is a cursor.</returns>
  186. Task<IAsyncCursor<string>> ListCollectionNamesAsync(ListCollectionNamesOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  187. /// <summary>
  188. /// Lists the names of all the collections in the database.
  189. /// </summary>
  190. /// <param name="session">The session.</param>
  191. /// <param name="options">The options.</param>
  192. /// <param name="cancellationToken">The cancellation token.</param>
  193. /// <returns>A Task whose result is a cursor.</returns>
  194. Task<IAsyncCursor<string>> ListCollectionNamesAsync(IClientSessionHandle session, ListCollectionNamesOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  195. /// <summary>
  196. /// Lists all the collections in the database.
  197. /// </summary>
  198. /// <param name="options">The options.</param>
  199. /// <param name="cancellationToken">The cancellation token.</param>
  200. /// <returns>A cursor.</returns>
  201. IAsyncCursor<BsonDocument> ListCollections(ListCollectionsOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  202. /// <summary>
  203. /// Lists all the collections in the database.
  204. /// </summary>
  205. /// <param name="session">The session.</param>
  206. /// <param name="options">The options.</param>
  207. /// <param name="cancellationToken">The cancellation token.</param>
  208. /// <returns>
  209. /// A cursor.
  210. /// </returns>
  211. IAsyncCursor<BsonDocument> ListCollections(IClientSessionHandle session, ListCollectionsOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  212. /// <summary>
  213. /// Lists all the collections in the database.
  214. /// </summary>
  215. /// <param name="options">The options.</param>
  216. /// <param name="cancellationToken">The cancellation token.</param>
  217. /// <returns>A Task whose result is a cursor.</returns>
  218. Task<IAsyncCursor<BsonDocument>> ListCollectionsAsync(ListCollectionsOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  219. /// <summary>
  220. /// Lists all the collections in the database.
  221. /// </summary>
  222. /// <param name="session">The session.</param>
  223. /// <param name="options">The options.</param>
  224. /// <param name="cancellationToken">The cancellation token.</param>
  225. /// <returns>
  226. /// A Task whose result is a cursor.
  227. /// </returns>
  228. Task<IAsyncCursor<BsonDocument>> ListCollectionsAsync(IClientSessionHandle session, ListCollectionsOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  229. /// <summary>
  230. /// Renames the collection.
  231. /// </summary>
  232. /// <param name="oldName">The old name.</param>
  233. /// <param name="newName">The new name.</param>
  234. /// <param name="options">The options.</param>
  235. /// <param name="cancellationToken">The cancellation token.</param>
  236. void RenameCollection(string oldName, string newName, RenameCollectionOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  237. /// <summary>
  238. /// Renames the collection.
  239. /// </summary>
  240. /// <param name="session">The session.</param>
  241. /// <param name="oldName">The old name.</param>
  242. /// <param name="newName">The new name.</param>
  243. /// <param name="options">The options.</param>
  244. /// <param name="cancellationToken">The cancellation token.</param>
  245. void RenameCollection(IClientSessionHandle session, string oldName, string newName, RenameCollectionOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  246. /// <summary>
  247. /// Renames the collection.
  248. /// </summary>
  249. /// <param name="oldName">The old name.</param>
  250. /// <param name="newName">The new name.</param>
  251. /// <param name="options">The options.</param>
  252. /// <param name="cancellationToken">The cancellation token.</param>
  253. /// <returns>A task.</returns>
  254. Task RenameCollectionAsync(string oldName, string newName, RenameCollectionOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  255. /// <summary>
  256. /// Renames the collection.
  257. /// </summary>
  258. /// <param name="session">The session.</param>
  259. /// <param name="oldName">The old name.</param>
  260. /// <param name="newName">The new name.</param>
  261. /// <param name="options">The options.</param>
  262. /// <param name="cancellationToken">The cancellation token.</param>
  263. /// <returns>
  264. /// A task.
  265. /// </returns>
  266. Task RenameCollectionAsync(IClientSessionHandle session, string oldName, string newName, RenameCollectionOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  267. /// <summary>
  268. /// Runs a command.
  269. /// </summary>
  270. /// <typeparam name="TResult">The result type of the command.</typeparam>
  271. /// <param name="command">The command.</param>
  272. /// <param name="readPreference">The read preference.</param>
  273. /// <param name="cancellationToken">The cancellation token.</param>
  274. /// <returns>
  275. /// The result of the command.
  276. /// </returns>
  277. TResult RunCommand<TResult>(Command<TResult> command, ReadPreference readPreference = null, CancellationToken cancellationToken = default(CancellationToken));
  278. /// <summary>
  279. /// Runs a command.
  280. /// </summary>
  281. /// <typeparam name="TResult">The result type of the command.</typeparam>
  282. /// <param name="session">The session.</param>
  283. /// <param name="command">The command.</param>
  284. /// <param name="readPreference">The read preference.</param>
  285. /// <param name="cancellationToken">The cancellation token.</param>
  286. /// <returns>
  287. /// The result of the command.
  288. /// </returns>
  289. TResult RunCommand<TResult>(IClientSessionHandle session, Command<TResult> command, ReadPreference readPreference = null, CancellationToken cancellationToken = default(CancellationToken));
  290. /// <summary>
  291. /// Runs a command.
  292. /// </summary>
  293. /// <typeparam name="TResult">The result type of the command.</typeparam>
  294. /// <param name="command">The command.</param>
  295. /// <param name="readPreference">The read preference.</param>
  296. /// <param name="cancellationToken">The cancellation token.</param>
  297. /// <returns>
  298. /// The result of the command.
  299. /// </returns>
  300. Task<TResult> RunCommandAsync<TResult>(Command<TResult> command, ReadPreference readPreference = null, CancellationToken cancellationToken = default(CancellationToken));
  301. /// <summary>
  302. /// Runs a command.
  303. /// </summary>
  304. /// <typeparam name="TResult">The result type of the command.</typeparam>
  305. /// <param name="session">The session.</param>
  306. /// <param name="command">The command.</param>
  307. /// <param name="readPreference">The read preference.</param>
  308. /// <param name="cancellationToken">The cancellation token.</param>
  309. /// <returns>
  310. /// The result of the command.
  311. /// </returns>
  312. Task<TResult> RunCommandAsync<TResult>(IClientSessionHandle session, Command<TResult> command, ReadPreference readPreference = null, CancellationToken cancellationToken = default(CancellationToken));
  313. /// <summary>
  314. /// Watches changes on all collections in a database.
  315. /// </summary>
  316. /// <typeparam name="TResult">The type of the result.</typeparam>
  317. /// <param name="pipeline">The pipeline.</param>
  318. /// <param name="options">The options.</param>
  319. /// <param name="cancellationToken">The cancellation token.</param>
  320. /// <returns>
  321. /// A change stream.
  322. /// </returns>
  323. IAsyncCursor<TResult> Watch<TResult>(
  324. PipelineDefinition<ChangeStreamDocument<BsonDocument>, TResult> pipeline,
  325. ChangeStreamOptions options = null,
  326. CancellationToken cancellationToken = default(CancellationToken));
  327. /// <summary>
  328. /// Watches changes on all collections in a database.
  329. /// </summary>
  330. /// <typeparam name="TResult">The type of the result.</typeparam>
  331. /// <param name="session">The session.</param>
  332. /// <param name="pipeline">The pipeline.</param>
  333. /// <param name="options">The options.</param>
  334. /// <param name="cancellationToken">The cancellation token.</param>
  335. /// <returns>
  336. /// A change stream.
  337. /// </returns>
  338. IAsyncCursor<TResult> Watch<TResult>(
  339. IClientSessionHandle session,
  340. PipelineDefinition<ChangeStreamDocument<BsonDocument>, TResult> pipeline,
  341. ChangeStreamOptions options = null,
  342. CancellationToken cancellationToken = default(CancellationToken));
  343. /// <summary>
  344. /// Watches changes on all collections in a database.
  345. /// </summary>
  346. /// <typeparam name="TResult">The type of the result.</typeparam>
  347. /// <param name="pipeline">The pipeline.</param>
  348. /// <param name="options">The options.</param>
  349. /// <param name="cancellationToken">The cancellation token.</param>
  350. /// <returns>
  351. /// A change stream.
  352. /// </returns>
  353. Task<IAsyncCursor<TResult>> WatchAsync<TResult>(
  354. PipelineDefinition<ChangeStreamDocument<BsonDocument>, TResult> pipeline,
  355. ChangeStreamOptions options = null,
  356. CancellationToken cancellationToken = default(CancellationToken));
  357. /// <summary>
  358. /// Watches changes on all collections in a database.
  359. /// </summary>
  360. /// <typeparam name="TResult">The type of the result.</typeparam>
  361. /// <param name="session">The session.</param>
  362. /// <param name="pipeline">The pipeline.</param>
  363. /// <param name="options">The options.</param>
  364. /// <param name="cancellationToken">The cancellation token.</param>
  365. /// <returns>
  366. /// A change stream.
  367. /// </returns>
  368. Task<IAsyncCursor<TResult>> WatchAsync<TResult>(
  369. IClientSessionHandle session,
  370. PipelineDefinition<ChangeStreamDocument<BsonDocument>, TResult> pipeline,
  371. ChangeStreamOptions options = null,
  372. CancellationToken cancellationToken = default(CancellationToken));
  373. /// <summary>
  374. /// Returns a new IMongoDatabase instance with a different read concern setting.
  375. /// </summary>
  376. /// <param name="readConcern">The read concern.</param>
  377. /// <returns>A new IMongoDatabase instance with a different read concern setting.</returns>
  378. IMongoDatabase WithReadConcern(ReadConcern readConcern);
  379. /// <summary>
  380. /// Returns a new IMongoDatabase instance with a different read preference setting.
  381. /// </summary>
  382. /// <param name="readPreference">The read preference.</param>
  383. /// <returns>A new IMongoDatabase instance with a different read preference setting.</returns>
  384. IMongoDatabase WithReadPreference(ReadPreference readPreference);
  385. /// <summary>
  386. /// Returns a new IMongoDatabase instance with a different write concern setting.
  387. /// </summary>
  388. /// <param name="writeConcern">The write concern.</param>
  389. /// <returns>A new IMongoDatabase instance with a different write concern setting.</returns>
  390. IMongoDatabase WithWriteConcern(WriteConcern writeConcern);
  391. }
  392. }