collection.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Collection = void 0;
  4. const bson_1 = require("./bson");
  5. const ordered_1 = require("./bulk/ordered");
  6. const unordered_1 = require("./bulk/unordered");
  7. const change_stream_1 = require("./change_stream");
  8. const aggregation_cursor_1 = require("./cursor/aggregation_cursor");
  9. const find_cursor_1 = require("./cursor/find_cursor");
  10. const error_1 = require("./error");
  11. const bulk_write_1 = require("./operations/bulk_write");
  12. const count_1 = require("./operations/count");
  13. const count_documents_1 = require("./operations/count_documents");
  14. const delete_1 = require("./operations/delete");
  15. const distinct_1 = require("./operations/distinct");
  16. const drop_1 = require("./operations/drop");
  17. const estimated_document_count_1 = require("./operations/estimated_document_count");
  18. const execute_operation_1 = require("./operations/execute_operation");
  19. const find_and_modify_1 = require("./operations/find_and_modify");
  20. const indexes_1 = require("./operations/indexes");
  21. const insert_1 = require("./operations/insert");
  22. const is_capped_1 = require("./operations/is_capped");
  23. const map_reduce_1 = require("./operations/map_reduce");
  24. const options_operation_1 = require("./operations/options_operation");
  25. const rename_1 = require("./operations/rename");
  26. const stats_1 = require("./operations/stats");
  27. const update_1 = require("./operations/update");
  28. const read_concern_1 = require("./read_concern");
  29. const read_preference_1 = require("./read_preference");
  30. const utils_1 = require("./utils");
  31. const write_concern_1 = require("./write_concern");
  32. /**
  33. * The **Collection** class is an internal class that embodies a MongoDB collection
  34. * allowing for insert/update/remove/find and other command operation on that MongoDB collection.
  35. *
  36. * **COLLECTION Cannot directly be instantiated**
  37. * @public
  38. *
  39. * @example
  40. * ```js
  41. * const MongoClient = require('mongodb').MongoClient;
  42. * const test = require('assert');
  43. * // Connection url
  44. * const url = 'mongodb://localhost:27017';
  45. * // Database Name
  46. * const dbName = 'test';
  47. * // Connect using MongoClient
  48. * MongoClient.connect(url, function(err, client) {
  49. * // Create a collection we want to drop later
  50. * const col = client.db(dbName).collection('createIndexExample1');
  51. * // Show that duplicate records got dropped
  52. * col.find({}).toArray(function(err, items) {
  53. * expect(err).to.not.exist;
  54. * test.equal(4, items.length);
  55. * client.close();
  56. * });
  57. * });
  58. * ```
  59. */
  60. class Collection {
  61. /**
  62. * Create a new Collection instance
  63. * @internal
  64. */
  65. constructor(db, name, options) {
  66. var _a, _b;
  67. (0, utils_1.checkCollectionName)(name);
  68. // Internal state
  69. this.s = {
  70. db,
  71. options,
  72. namespace: new utils_1.MongoDBNamespace(db.databaseName, name),
  73. pkFactory: (_b = (_a = db.options) === null || _a === void 0 ? void 0 : _a.pkFactory) !== null && _b !== void 0 ? _b : utils_1.DEFAULT_PK_FACTORY,
  74. readPreference: read_preference_1.ReadPreference.fromOptions(options),
  75. bsonOptions: (0, bson_1.resolveBSONOptions)(options, db),
  76. readConcern: read_concern_1.ReadConcern.fromOptions(options),
  77. writeConcern: write_concern_1.WriteConcern.fromOptions(options)
  78. };
  79. }
  80. /**
  81. * The name of the database this collection belongs to
  82. */
  83. get dbName() {
  84. return this.s.namespace.db;
  85. }
  86. /**
  87. * The name of this collection
  88. */
  89. get collectionName() {
  90. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  91. return this.s.namespace.collection;
  92. }
  93. /**
  94. * The namespace of this collection, in the format `${this.dbName}.${this.collectionName}`
  95. */
  96. get namespace() {
  97. return this.s.namespace.toString();
  98. }
  99. /**
  100. * The current readConcern of the collection. If not explicitly defined for
  101. * this collection, will be inherited from the parent DB
  102. */
  103. get readConcern() {
  104. if (this.s.readConcern == null) {
  105. return this.s.db.readConcern;
  106. }
  107. return this.s.readConcern;
  108. }
  109. /**
  110. * The current readPreference of the collection. If not explicitly defined for
  111. * this collection, will be inherited from the parent DB
  112. */
  113. get readPreference() {
  114. if (this.s.readPreference == null) {
  115. return this.s.db.readPreference;
  116. }
  117. return this.s.readPreference;
  118. }
  119. get bsonOptions() {
  120. return this.s.bsonOptions;
  121. }
  122. /**
  123. * The current writeConcern of the collection. If not explicitly defined for
  124. * this collection, will be inherited from the parent DB
  125. */
  126. get writeConcern() {
  127. if (this.s.writeConcern == null) {
  128. return this.s.db.writeConcern;
  129. }
  130. return this.s.writeConcern;
  131. }
  132. /** The current index hint for the collection */
  133. get hint() {
  134. return this.s.collectionHint;
  135. }
  136. set hint(v) {
  137. this.s.collectionHint = (0, utils_1.normalizeHintField)(v);
  138. }
  139. insertOne(doc, options, callback) {
  140. if (typeof options === 'function') {
  141. callback = options;
  142. options = {};
  143. }
  144. // CSFLE passes in { w: 'majority' } to ensure the lib works in both 3.x and 4.x
  145. // we support that option style here only
  146. if (options && Reflect.get(options, 'w')) {
  147. options.writeConcern = write_concern_1.WriteConcern.fromOptions(Reflect.get(options, 'w'));
  148. }
  149. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new insert_1.InsertOneOperation(this, doc, (0, utils_1.resolveOptions)(this, options)), callback);
  150. }
  151. insertMany(docs, options, callback) {
  152. if (typeof options === 'function')
  153. (callback = options), (options = {});
  154. options = options ? Object.assign({}, options) : { ordered: true };
  155. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new insert_1.InsertManyOperation(this, docs, (0, utils_1.resolveOptions)(this, options)), callback);
  156. }
  157. bulkWrite(operations, options, callback) {
  158. if (typeof options === 'function')
  159. (callback = options), (options = {});
  160. options = options || { ordered: true };
  161. if (!Array.isArray(operations)) {
  162. throw new error_1.MongoInvalidArgumentError('Argument "operations" must be an array of documents');
  163. }
  164. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new bulk_write_1.BulkWriteOperation(this, operations, (0, utils_1.resolveOptions)(this, options)), callback);
  165. }
  166. updateOne(filter, update, options, callback) {
  167. if (typeof options === 'function')
  168. (callback = options), (options = {});
  169. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new update_1.UpdateOneOperation(this, filter, update, (0, utils_1.resolveOptions)(this, options)), callback);
  170. }
  171. replaceOne(filter, replacement, options, callback) {
  172. if (typeof options === 'function')
  173. (callback = options), (options = {});
  174. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new update_1.ReplaceOneOperation(this, filter, replacement, (0, utils_1.resolveOptions)(this, options)), callback);
  175. }
  176. updateMany(filter, update, options, callback) {
  177. if (typeof options === 'function')
  178. (callback = options), (options = {});
  179. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new update_1.UpdateManyOperation(this, filter, update, (0, utils_1.resolveOptions)(this, options)), callback);
  180. }
  181. deleteOne(filter, options, callback) {
  182. if (typeof options === 'function')
  183. (callback = options), (options = {});
  184. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new delete_1.DeleteOneOperation(this, filter, (0, utils_1.resolveOptions)(this, options)), callback);
  185. }
  186. deleteMany(filter, options, callback) {
  187. if (filter == null) {
  188. filter = {};
  189. options = {};
  190. callback = undefined;
  191. }
  192. else if (typeof filter === 'function') {
  193. callback = filter;
  194. filter = {};
  195. options = {};
  196. }
  197. else if (typeof options === 'function') {
  198. callback = options;
  199. options = {};
  200. }
  201. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new delete_1.DeleteManyOperation(this, filter, (0, utils_1.resolveOptions)(this, options)), callback);
  202. }
  203. rename(newName, options, callback) {
  204. if (typeof options === 'function')
  205. (callback = options), (options = {});
  206. // Intentionally, we do not inherit options from parent for this operation.
  207. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new rename_1.RenameOperation(this, newName, {
  208. ...options,
  209. readPreference: read_preference_1.ReadPreference.PRIMARY
  210. }), callback);
  211. }
  212. drop(options, callback) {
  213. if (typeof options === 'function')
  214. (callback = options), (options = {});
  215. options = options !== null && options !== void 0 ? options : {};
  216. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new drop_1.DropCollectionOperation(this.s.db, this.collectionName, options), callback);
  217. }
  218. findOne(filter, options, callback) {
  219. if (callback != null && typeof callback !== 'function') {
  220. throw new error_1.MongoInvalidArgumentError('Third parameter to `findOne()` must be a callback or undefined');
  221. }
  222. if (typeof filter === 'function') {
  223. callback = filter;
  224. filter = {};
  225. options = {};
  226. }
  227. if (typeof options === 'function') {
  228. callback = options;
  229. options = {};
  230. }
  231. const finalFilter = filter !== null && filter !== void 0 ? filter : {};
  232. const finalOptions = options !== null && options !== void 0 ? options : {};
  233. return this.find(finalFilter, finalOptions).limit(-1).batchSize(1).next(callback);
  234. }
  235. find(filter, options) {
  236. if (arguments.length > 2) {
  237. throw new error_1.MongoInvalidArgumentError('Method "collection.find()" accepts at most two arguments');
  238. }
  239. if (typeof options === 'function') {
  240. throw new error_1.MongoInvalidArgumentError('Argument "options" must not be function');
  241. }
  242. return new find_cursor_1.FindCursor(this.s.db.s.client, this.s.namespace, filter, (0, utils_1.resolveOptions)(this, options));
  243. }
  244. options(options, callback) {
  245. if (typeof options === 'function')
  246. (callback = options), (options = {});
  247. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new options_operation_1.OptionsOperation(this, (0, utils_1.resolveOptions)(this, options)), callback);
  248. }
  249. isCapped(options, callback) {
  250. if (typeof options === 'function')
  251. (callback = options), (options = {});
  252. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new is_capped_1.IsCappedOperation(this, (0, utils_1.resolveOptions)(this, options)), callback);
  253. }
  254. createIndex(indexSpec, options, callback) {
  255. if (typeof options === 'function')
  256. (callback = options), (options = {});
  257. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new indexes_1.CreateIndexOperation(this, this.collectionName, indexSpec, (0, utils_1.resolveOptions)(this, options)), callback);
  258. }
  259. createIndexes(indexSpecs, options, callback) {
  260. if (typeof options === 'function')
  261. (callback = options), (options = {});
  262. options = options ? Object.assign({}, options) : {};
  263. if (typeof options.maxTimeMS !== 'number')
  264. delete options.maxTimeMS;
  265. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new indexes_1.CreateIndexesOperation(this, this.collectionName, indexSpecs, (0, utils_1.resolveOptions)(this, options)), callback);
  266. }
  267. dropIndex(indexName, options, callback) {
  268. if (typeof options === 'function')
  269. (callback = options), (options = {});
  270. options = (0, utils_1.resolveOptions)(this, options);
  271. // Run only against primary
  272. options.readPreference = read_preference_1.ReadPreference.primary;
  273. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new indexes_1.DropIndexOperation(this, indexName, options), callback);
  274. }
  275. dropIndexes(options, callback) {
  276. if (typeof options === 'function')
  277. (callback = options), (options = {});
  278. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new indexes_1.DropIndexesOperation(this, (0, utils_1.resolveOptions)(this, options)), callback);
  279. }
  280. /**
  281. * Get the list of all indexes information for the collection.
  282. *
  283. * @param options - Optional settings for the command
  284. */
  285. listIndexes(options) {
  286. return new indexes_1.ListIndexesCursor(this, (0, utils_1.resolveOptions)(this, options));
  287. }
  288. indexExists(indexes, options, callback) {
  289. if (typeof options === 'function')
  290. (callback = options), (options = {});
  291. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new indexes_1.IndexExistsOperation(this, indexes, (0, utils_1.resolveOptions)(this, options)), callback);
  292. }
  293. indexInformation(options, callback) {
  294. if (typeof options === 'function')
  295. (callback = options), (options = {});
  296. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new indexes_1.IndexInformationOperation(this.s.db, this.collectionName, (0, utils_1.resolveOptions)(this, options)), callback);
  297. }
  298. estimatedDocumentCount(options, callback) {
  299. if (typeof options === 'function')
  300. (callback = options), (options = {});
  301. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new estimated_document_count_1.EstimatedDocumentCountOperation(this, (0, utils_1.resolveOptions)(this, options)), callback);
  302. }
  303. countDocuments(filter, options, callback) {
  304. if (filter == null) {
  305. (filter = {}), (options = {}), (callback = undefined);
  306. }
  307. else if (typeof filter === 'function') {
  308. (callback = filter), (filter = {}), (options = {});
  309. }
  310. else {
  311. if (arguments.length === 2) {
  312. if (typeof options === 'function')
  313. (callback = options), (options = {});
  314. }
  315. }
  316. filter !== null && filter !== void 0 ? filter : (filter = {});
  317. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new count_documents_1.CountDocumentsOperation(this, filter, (0, utils_1.resolveOptions)(this, options)), callback);
  318. }
  319. // Implementation
  320. distinct(key, filter, options, callback) {
  321. if (typeof filter === 'function') {
  322. (callback = filter), (filter = {}), (options = {});
  323. }
  324. else {
  325. if (arguments.length === 3 && typeof options === 'function') {
  326. (callback = options), (options = {});
  327. }
  328. }
  329. filter !== null && filter !== void 0 ? filter : (filter = {});
  330. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new distinct_1.DistinctOperation(this, key, filter, (0, utils_1.resolveOptions)(this, options)), callback);
  331. }
  332. indexes(options, callback) {
  333. if (typeof options === 'function')
  334. (callback = options), (options = {});
  335. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new indexes_1.IndexesOperation(this, (0, utils_1.resolveOptions)(this, options)), callback);
  336. }
  337. stats(options, callback) {
  338. if (typeof options === 'function')
  339. (callback = options), (options = {});
  340. options = options !== null && options !== void 0 ? options : {};
  341. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new stats_1.CollStatsOperation(this, options), callback);
  342. }
  343. findOneAndDelete(filter, options, callback) {
  344. if (typeof options === 'function')
  345. (callback = options), (options = {});
  346. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new find_and_modify_1.FindOneAndDeleteOperation(this, filter, (0, utils_1.resolveOptions)(this, options)), callback);
  347. }
  348. findOneAndReplace(filter, replacement, options, callback) {
  349. if (typeof options === 'function')
  350. (callback = options), (options = {});
  351. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new find_and_modify_1.FindOneAndReplaceOperation(this, filter, replacement, (0, utils_1.resolveOptions)(this, options)), callback);
  352. }
  353. findOneAndUpdate(filter, update, options, callback) {
  354. if (typeof options === 'function')
  355. (callback = options), (options = {});
  356. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new find_and_modify_1.FindOneAndUpdateOperation(this, filter, update, (0, utils_1.resolveOptions)(this, options)), callback);
  357. }
  358. /**
  359. * Execute an aggregation framework pipeline against the collection, needs MongoDB \>= 2.2
  360. *
  361. * @param pipeline - An array of aggregation pipelines to execute
  362. * @param options - Optional settings for the command
  363. */
  364. aggregate(pipeline = [], options) {
  365. if (arguments.length > 2) {
  366. throw new error_1.MongoInvalidArgumentError('Method "collection.aggregate()" accepts at most two arguments');
  367. }
  368. if (!Array.isArray(pipeline)) {
  369. throw new error_1.MongoInvalidArgumentError('Argument "pipeline" must be an array of aggregation stages');
  370. }
  371. if (typeof options === 'function') {
  372. throw new error_1.MongoInvalidArgumentError('Argument "options" must not be function');
  373. }
  374. return new aggregation_cursor_1.AggregationCursor(this.s.db.s.client, this.s.namespace, pipeline, (0, utils_1.resolveOptions)(this, options));
  375. }
  376. /**
  377. * Create a new Change Stream, watching for new changes (insertions, updates, replacements, deletions, and invalidations) in this collection.
  378. *
  379. * @remarks
  380. * watch() accepts two generic arguments for distinct usecases:
  381. * - The first is to override the schema that may be defined for this specific collection
  382. * - The second is to override the shape of the change stream document entirely, if it is not provided the type will default to ChangeStreamDocument of the first argument
  383. * @example
  384. * By just providing the first argument I can type the change to be `ChangeStreamDocument<{ _id: number }>`
  385. * ```ts
  386. * collection.watch<{ _id: number }>()
  387. * .on('change', change => console.log(change._id.toFixed(4)));
  388. * ```
  389. *
  390. * @example
  391. * Passing a second argument provides a way to reflect the type changes caused by an advanced pipeline.
  392. * Here, we are using a pipeline to have MongoDB filter for insert changes only and add a comment.
  393. * No need start from scratch on the ChangeStreamInsertDocument type!
  394. * By using an intersection we can save time and ensure defaults remain the same type!
  395. * ```ts
  396. * collection
  397. * .watch<Schema, ChangeStreamInsertDocument<Schema> & { comment: string }>([
  398. * { $addFields: { comment: 'big changes' } },
  399. * { $match: { operationType: 'insert' } }
  400. * ])
  401. * .on('change', change => {
  402. * change.comment.startsWith('big');
  403. * change.operationType === 'insert';
  404. * // No need to narrow in code because the generics did that for us!
  405. * expectType<Schema>(change.fullDocument);
  406. * });
  407. * ```
  408. *
  409. * @param pipeline - An array of {@link https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/|aggregation pipeline stages} through which to pass change stream documents. This allows for filtering (using $match) and manipulating the change stream documents.
  410. * @param options - Optional settings for the command
  411. * @typeParam TLocal - Type of the data being detected by the change stream
  412. * @typeParam TChange - Type of the whole change stream document emitted
  413. */
  414. watch(pipeline = [], options = {}) {
  415. // Allow optionally not specifying a pipeline
  416. if (!Array.isArray(pipeline)) {
  417. options = pipeline;
  418. pipeline = [];
  419. }
  420. return new change_stream_1.ChangeStream(this, pipeline, (0, utils_1.resolveOptions)(this, options));
  421. }
  422. mapReduce(map, reduce, options, callback) {
  423. (0, utils_1.emitWarningOnce)('collection.mapReduce is deprecated. Use the aggregation pipeline instead. Visit https://docs.mongodb.com/manual/reference/map-reduce-to-aggregation-pipeline for more information on how to translate map-reduce operations to the aggregation pipeline.');
  424. if ('function' === typeof options)
  425. (callback = options), (options = {});
  426. // Out must always be defined (make sure we don't break weirdly on pre 1.8+ servers)
  427. // TODO NODE-3339: Figure out if this is still necessary given we no longer officially support pre-1.8
  428. if ((options === null || options === void 0 ? void 0 : options.out) == null) {
  429. throw new error_1.MongoInvalidArgumentError('Option "out" must be defined, see mongodb docs for possible values');
  430. }
  431. if ('function' === typeof map) {
  432. map = map.toString();
  433. }
  434. if ('function' === typeof reduce) {
  435. reduce = reduce.toString();
  436. }
  437. if ('function' === typeof options.finalize) {
  438. options.finalize = options.finalize.toString();
  439. }
  440. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new map_reduce_1.MapReduceOperation(this, map, reduce, (0, utils_1.resolveOptions)(this, options)), callback);
  441. }
  442. /** Initiate an Out of order batch write operation. All operations will be buffered into insert/update/remove commands executed out of order. */
  443. initializeUnorderedBulkOp(options) {
  444. return new unordered_1.UnorderedBulkOperation(this, (0, utils_1.resolveOptions)(this, options));
  445. }
  446. /** Initiate an In order bulk write operation. Operations will be serially executed in the order they are added, creating a new operation for each switch in types. */
  447. initializeOrderedBulkOp(options) {
  448. return new ordered_1.OrderedBulkOperation(this, (0, utils_1.resolveOptions)(this, options));
  449. }
  450. /** Get the db scoped logger */
  451. getLogger() {
  452. return this.s.db.s.logger;
  453. }
  454. get logger() {
  455. return this.s.db.s.logger;
  456. }
  457. /**
  458. * Inserts a single document or a an array of documents into MongoDB. If documents passed in do not contain the **_id** field,
  459. * one will be added to each of the documents missing it by the driver, mutating the document. This behavior
  460. * can be overridden by setting the **forceServerObjectId** flag.
  461. *
  462. * @deprecated Use insertOne, insertMany or bulkWrite instead.
  463. * @param docs - The documents to insert
  464. * @param options - Optional settings for the command
  465. * @param callback - An optional callback, a Promise will be returned if none is provided
  466. */
  467. insert(docs, options, callback) {
  468. (0, utils_1.emitWarningOnce)('collection.insert is deprecated. Use insertOne, insertMany or bulkWrite instead.');
  469. if (typeof options === 'function')
  470. (callback = options), (options = {});
  471. options = options || { ordered: false };
  472. docs = !Array.isArray(docs) ? [docs] : docs;
  473. if (options.keepGoing === true) {
  474. options.ordered = false;
  475. }
  476. return this.insertMany(docs, options, callback);
  477. }
  478. /**
  479. * Updates documents.
  480. *
  481. * @deprecated use updateOne, updateMany or bulkWrite
  482. * @param selector - The selector for the update operation.
  483. * @param update - The update operations to be applied to the documents
  484. * @param options - Optional settings for the command
  485. * @param callback - An optional callback, a Promise will be returned if none is provided
  486. */
  487. update(selector, update, options, callback) {
  488. (0, utils_1.emitWarningOnce)('collection.update is deprecated. Use updateOne, updateMany, or bulkWrite instead.');
  489. if (typeof options === 'function')
  490. (callback = options), (options = {});
  491. options = options !== null && options !== void 0 ? options : {};
  492. return this.updateMany(selector, update, options, callback);
  493. }
  494. /**
  495. * Remove documents.
  496. *
  497. * @deprecated use deleteOne, deleteMany or bulkWrite
  498. * @param selector - The selector for the update operation.
  499. * @param options - Optional settings for the command
  500. * @param callback - An optional callback, a Promise will be returned if none is provided
  501. */
  502. remove(selector, options, callback) {
  503. (0, utils_1.emitWarningOnce)('collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead.');
  504. if (typeof options === 'function')
  505. (callback = options), (options = {});
  506. options = options !== null && options !== void 0 ? options : {};
  507. return this.deleteMany(selector, options, callback);
  508. }
  509. count(filter, options, callback) {
  510. if (typeof filter === 'function') {
  511. (callback = filter), (filter = {}), (options = {});
  512. }
  513. else {
  514. if (typeof options === 'function')
  515. (callback = options), (options = {});
  516. }
  517. filter !== null && filter !== void 0 ? filter : (filter = {});
  518. return (0, execute_operation_1.executeOperation)(this.s.db.s.client, new count_1.CountOperation(utils_1.MongoDBNamespace.fromString(this.namespace), filter, (0, utils_1.resolveOptions)(this, options)), callback);
  519. }
  520. }
  521. exports.Collection = Collection;
  522. //# sourceMappingURL=collection.js.map