mongodb 4.0.0 → 4.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (246) hide show
  1. package/README.md +62 -30
  2. package/lib/bson.js +1 -0
  3. package/lib/bson.js.map +1 -1
  4. package/lib/bulk/common.js +53 -30
  5. package/lib/bulk/common.js.map +1 -1
  6. package/lib/bulk/ordered.js +3 -2
  7. package/lib/bulk/ordered.js.map +1 -1
  8. package/lib/bulk/unordered.js +3 -2
  9. package/lib/bulk/unordered.js.map +1 -1
  10. package/lib/change_stream.js +23 -13
  11. package/lib/change_stream.js.map +1 -1
  12. package/lib/cmap/auth/auth_provider.js +2 -1
  13. package/lib/cmap/auth/auth_provider.js.map +1 -1
  14. package/lib/cmap/auth/gssapi.js +5 -4
  15. package/lib/cmap/auth/gssapi.js.map +1 -1
  16. package/lib/cmap/auth/mongo_credentials.js +9 -5
  17. package/lib/cmap/auth/mongo_credentials.js.map +1 -1
  18. package/lib/cmap/auth/mongocr.js +2 -2
  19. package/lib/cmap/auth/mongocr.js.map +1 -1
  20. package/lib/cmap/auth/mongodb_aws.js +32 -32
  21. package/lib/cmap/auth/mongodb_aws.js.map +1 -1
  22. package/lib/cmap/auth/plain.js +1 -1
  23. package/lib/cmap/auth/plain.js.map +1 -1
  24. package/lib/cmap/auth/scram.js +15 -12
  25. package/lib/cmap/auth/scram.js.map +1 -1
  26. package/lib/cmap/auth/x509.js +2 -2
  27. package/lib/cmap/auth/x509.js.map +1 -1
  28. package/lib/cmap/command_monitoring_events.js +26 -10
  29. package/lib/cmap/command_monitoring_events.js.map +1 -1
  30. package/lib/cmap/commands.js +9 -5
  31. package/lib/cmap/commands.js.map +1 -1
  32. package/lib/cmap/connect.js +23 -9
  33. package/lib/cmap/connect.js.map +1 -1
  34. package/lib/cmap/connection.js +43 -46
  35. package/lib/cmap/connection.js.map +1 -1
  36. package/lib/cmap/connection_pool.js +113 -15
  37. package/lib/cmap/connection_pool.js.map +1 -1
  38. package/lib/cmap/connection_pool_events.js +3 -1
  39. package/lib/cmap/connection_pool_events.js.map +1 -1
  40. package/lib/cmap/errors.js +3 -3
  41. package/lib/cmap/errors.js.map +1 -1
  42. package/lib/cmap/message_stream.js +1 -1
  43. package/lib/cmap/message_stream.js.map +1 -1
  44. package/lib/cmap/metrics.js +62 -0
  45. package/lib/cmap/metrics.js.map +1 -0
  46. package/lib/cmap/stream_description.js +3 -1
  47. package/lib/cmap/stream_description.js.map +1 -1
  48. package/lib/cmap/wire_protocol/compression.js +22 -9
  49. package/lib/cmap/wire_protocol/compression.js.map +1 -1
  50. package/lib/cmap/wire_protocol/shared.js +1 -1
  51. package/lib/cmap/wire_protocol/shared.js.map +1 -1
  52. package/lib/collection.js +23 -18
  53. package/lib/collection.js.map +1 -1
  54. package/lib/connection_string.js +76 -30
  55. package/lib/connection_string.js.map +1 -1
  56. package/lib/cursor/abstract_cursor.js +75 -68
  57. package/lib/cursor/abstract_cursor.js.map +1 -1
  58. package/lib/cursor/aggregation_cursor.js +47 -9
  59. package/lib/cursor/aggregation_cursor.js.map +1 -1
  60. package/lib/cursor/find_cursor.js +53 -13
  61. package/lib/cursor/find_cursor.js.map +1 -1
  62. package/lib/db.js +21 -14
  63. package/lib/db.js.map +1 -1
  64. package/lib/deps.js +16 -5
  65. package/lib/deps.js.map +1 -1
  66. package/lib/encrypter.js +5 -8
  67. package/lib/encrypter.js.map +1 -1
  68. package/lib/error.js +230 -34
  69. package/lib/error.js.map +1 -1
  70. package/lib/explain.js +2 -2
  71. package/lib/explain.js.map +1 -1
  72. package/lib/gridfs/download.js +22 -47
  73. package/lib/gridfs/download.js.map +1 -1
  74. package/lib/gridfs/index.js +4 -3
  75. package/lib/gridfs/index.js.map +1 -1
  76. package/lib/gridfs/upload.js +13 -21
  77. package/lib/gridfs/upload.js.map +1 -1
  78. package/lib/index.js +27 -2
  79. package/lib/index.js.map +1 -1
  80. package/lib/logger.js +3 -2
  81. package/lib/logger.js.map +1 -1
  82. package/lib/mongo_client.js +5 -8
  83. package/lib/mongo_client.js.map +1 -1
  84. package/lib/mongo_types.js.map +1 -1
  85. package/lib/operations/add_user.js +2 -3
  86. package/lib/operations/add_user.js.map +1 -1
  87. package/lib/operations/aggregate.js +12 -9
  88. package/lib/operations/aggregate.js.map +1 -1
  89. package/lib/operations/command.js +5 -7
  90. package/lib/operations/command.js.map +1 -1
  91. package/lib/operations/common_functions.js +1 -1
  92. package/lib/operations/common_functions.js.map +1 -1
  93. package/lib/operations/connect.js +3 -2
  94. package/lib/operations/connect.js.map +1 -1
  95. package/lib/operations/count.js +1 -1
  96. package/lib/operations/count.js.map +1 -1
  97. package/lib/operations/count_documents.js +1 -1
  98. package/lib/operations/count_documents.js.map +1 -1
  99. package/lib/operations/delete.js +5 -5
  100. package/lib/operations/delete.js.map +1 -1
  101. package/lib/operations/distinct.js +2 -2
  102. package/lib/operations/distinct.js.map +1 -1
  103. package/lib/operations/estimated_document_count.js +5 -1
  104. package/lib/operations/estimated_document_count.js.map +1 -1
  105. package/lib/operations/eval.js.map +1 -1
  106. package/lib/operations/execute_operation.js +31 -17
  107. package/lib/operations/execute_operation.js.map +1 -1
  108. package/lib/operations/find.js +13 -9
  109. package/lib/operations/find.js.map +1 -1
  110. package/lib/operations/find_and_modify.js +9 -9
  111. package/lib/operations/find_and_modify.js.map +1 -1
  112. package/lib/operations/indexes.js +8 -3
  113. package/lib/operations/indexes.js.map +1 -1
  114. package/lib/operations/insert.js +5 -3
  115. package/lib/operations/insert.js.map +1 -1
  116. package/lib/operations/is_capped.js +2 -1
  117. package/lib/operations/is_capped.js.map +1 -1
  118. package/lib/operations/list_collections.js +6 -3
  119. package/lib/operations/list_collections.js.map +1 -1
  120. package/lib/operations/map_reduce.js +1 -1
  121. package/lib/operations/map_reduce.js.map +1 -1
  122. package/lib/operations/operation.js +3 -1
  123. package/lib/operations/operation.js.map +1 -1
  124. package/lib/operations/options_operation.js +2 -1
  125. package/lib/operations/options_operation.js.map +1 -1
  126. package/lib/operations/profiling_level.js +4 -2
  127. package/lib/operations/profiling_level.js.map +1 -1
  128. package/lib/operations/set_profiling_level.js +4 -2
  129. package/lib/operations/set_profiling_level.js.map +1 -1
  130. package/lib/operations/update.js +12 -12
  131. package/lib/operations/update.js.map +1 -1
  132. package/lib/operations/validate_collection.js +6 -5
  133. package/lib/operations/validate_collection.js.map +1 -1
  134. package/lib/promise_provider.js +1 -1
  135. package/lib/promise_provider.js.map +1 -1
  136. package/lib/read_preference.js +8 -8
  137. package/lib/read_preference.js.map +1 -1
  138. package/lib/sdam/common.js +12 -10
  139. package/lib/sdam/common.js.map +1 -1
  140. package/lib/sdam/server.js +90 -25
  141. package/lib/sdam/server.js.map +1 -1
  142. package/lib/sdam/server_description.js +9 -4
  143. package/lib/sdam/server_description.js.map +1 -1
  144. package/lib/sdam/server_selection.js +10 -4
  145. package/lib/sdam/server_selection.js.map +1 -1
  146. package/lib/sdam/srv_polling.js +1 -1
  147. package/lib/sdam/srv_polling.js.map +1 -1
  148. package/lib/sdam/topology.js +42 -21
  149. package/lib/sdam/topology.js.map +1 -1
  150. package/lib/sdam/topology_description.js +7 -3
  151. package/lib/sdam/topology_description.js.map +1 -1
  152. package/lib/sessions.js +132 -31
  153. package/lib/sessions.js.map +1 -1
  154. package/lib/sort.js +3 -3
  155. package/lib/sort.js.map +1 -1
  156. package/lib/transactions.js +15 -7
  157. package/lib/transactions.js.map +1 -1
  158. package/lib/utils.js +60 -20
  159. package/lib/utils.js.map +1 -1
  160. package/mongodb.d.ts +523 -138
  161. package/mongodb.ts34.d.ts +480 -141
  162. package/package.json +44 -48
  163. package/src/bson.ts +1 -0
  164. package/src/bulk/common.ts +83 -43
  165. package/src/bulk/ordered.ts +4 -3
  166. package/src/bulk/unordered.ts +4 -3
  167. package/src/change_stream.ts +46 -29
  168. package/src/cmap/auth/auth_provider.ts +3 -2
  169. package/src/cmap/auth/gssapi.ts +15 -5
  170. package/src/cmap/auth/mongo_credentials.ts +22 -8
  171. package/src/cmap/auth/mongocr.ts +3 -3
  172. package/src/cmap/auth/mongodb_aws.ts +52 -39
  173. package/src/cmap/auth/plain.ts +2 -2
  174. package/src/cmap/auth/scram.ts +23 -13
  175. package/src/cmap/auth/x509.ts +3 -3
  176. package/src/cmap/command_monitoring_events.ts +36 -14
  177. package/src/cmap/commands.ts +12 -6
  178. package/src/cmap/connect.ts +42 -12
  179. package/src/cmap/connection.ts +54 -62
  180. package/src/cmap/connection_pool.ts +141 -20
  181. package/src/cmap/connection_pool_events.ts +8 -1
  182. package/src/cmap/errors.ts +3 -4
  183. package/src/cmap/message_stream.ts +2 -4
  184. package/src/cmap/metrics.ts +58 -0
  185. package/src/cmap/stream_description.ts +6 -1
  186. package/src/cmap/wire_protocol/compression.ts +26 -13
  187. package/src/cmap/wire_protocol/shared.ts +4 -2
  188. package/src/collection.ts +75 -70
  189. package/src/connection_string.ts +97 -34
  190. package/src/cursor/abstract_cursor.ts +141 -104
  191. package/src/cursor/aggregation_cursor.ts +34 -20
  192. package/src/cursor/find_cursor.ts +41 -21
  193. package/src/db.ts +19 -18
  194. package/src/deps.ts +110 -22
  195. package/src/encrypter.ts +6 -12
  196. package/src/error.ts +264 -48
  197. package/src/explain.ts +3 -3
  198. package/src/gridfs/download.ts +48 -53
  199. package/src/gridfs/index.ts +5 -4
  200. package/src/gridfs/upload.ts +32 -33
  201. package/src/index.ts +42 -4
  202. package/src/logger.ts +6 -3
  203. package/src/mongo_client.ts +20 -23
  204. package/src/mongo_types.ts +19 -20
  205. package/src/operations/add_user.ts +4 -5
  206. package/src/operations/aggregate.ts +18 -17
  207. package/src/operations/command.ts +7 -10
  208. package/src/operations/common_functions.ts +2 -3
  209. package/src/operations/connect.ts +4 -3
  210. package/src/operations/count.ts +2 -2
  211. package/src/operations/count_documents.ts +2 -2
  212. package/src/operations/delete.ts +8 -6
  213. package/src/operations/distinct.ts +5 -3
  214. package/src/operations/estimated_document_count.ts +5 -1
  215. package/src/operations/eval.ts +1 -1
  216. package/src/operations/execute_operation.ts +41 -20
  217. package/src/operations/find.ts +25 -16
  218. package/src/operations/find_and_modify.ts +12 -10
  219. package/src/operations/indexes.ts +39 -8
  220. package/src/operations/insert.ts +7 -4
  221. package/src/operations/is_capped.ts +3 -2
  222. package/src/operations/list_collections.ts +9 -6
  223. package/src/operations/map_reduce.ts +4 -2
  224. package/src/operations/operation.ts +7 -2
  225. package/src/operations/options_operation.ts +3 -2
  226. package/src/operations/profiling_level.ts +5 -3
  227. package/src/operations/set_profiling_level.ts +9 -3
  228. package/src/operations/update.ts +17 -13
  229. package/src/operations/validate_collection.ts +7 -6
  230. package/src/promise_provider.ts +2 -2
  231. package/src/read_preference.ts +11 -9
  232. package/src/sdam/common.ts +11 -9
  233. package/src/sdam/server.ts +168 -69
  234. package/src/sdam/server_description.ts +16 -4
  235. package/src/sdam/server_selection.ts +15 -7
  236. package/src/sdam/srv_polling.ts +2 -2
  237. package/src/sdam/topology.ts +67 -36
  238. package/src/sdam/topology_description.ts +11 -4
  239. package/src/sessions.ts +194 -37
  240. package/src/sort.ts +6 -4
  241. package/src/transactions.ts +18 -9
  242. package/src/utils.ts +73 -20
  243. package/HISTORY.md +0 -2993
  244. package/lib/operations/find_one.js +0 -34
  245. package/lib/operations/find_one.js.map +0 -1
  246. package/src/operations/find_one.ts +0 -43
@@ -0,0 +1,58 @@
1
+ /** @internal */
2
+ export class ConnectionPoolMetrics {
3
+ static readonly TXN = 'txn' as const;
4
+ static readonly CURSOR = 'cursor' as const;
5
+ static readonly OTHER = 'other' as const;
6
+
7
+ txnConnections = 0;
8
+ cursorConnections = 0;
9
+ otherConnections = 0;
10
+
11
+ /**
12
+ * Mark a connection as pinned for a specific operation.
13
+ */
14
+ markPinned(pinType: string): void {
15
+ if (pinType === ConnectionPoolMetrics.TXN) {
16
+ this.txnConnections += 1;
17
+ } else if (pinType === ConnectionPoolMetrics.CURSOR) {
18
+ this.cursorConnections += 1;
19
+ } else {
20
+ this.otherConnections += 1;
21
+ }
22
+ }
23
+
24
+ /**
25
+ * Unmark a connection as pinned for an operation.
26
+ */
27
+ markUnpinned(pinType: string): void {
28
+ if (pinType === ConnectionPoolMetrics.TXN) {
29
+ this.txnConnections -= 1;
30
+ } else if (pinType === ConnectionPoolMetrics.CURSOR) {
31
+ this.cursorConnections -= 1;
32
+ } else {
33
+ this.otherConnections -= 1;
34
+ }
35
+ }
36
+
37
+ /**
38
+ * Return information about the cmap metrics as a string.
39
+ */
40
+ info(maxPoolSize: number): string {
41
+ return (
42
+ 'Timed out while checking out a connection from connection pool: ' +
43
+ `maxPoolSize: ${maxPoolSize}, ` +
44
+ `connections in use by cursors: ${this.cursorConnections}, ` +
45
+ `connections in use by transactions: ${this.txnConnections}, ` +
46
+ `connections in use by other operations: ${this.otherConnections}`
47
+ );
48
+ }
49
+
50
+ /**
51
+ * Reset the metrics to the initial values.
52
+ */
53
+ reset(): void {
54
+ this.txnConnections = 0;
55
+ this.cursorConnections = 0;
56
+ this.otherConnections = 0;
57
+ }
58
+ }
@@ -15,6 +15,8 @@ const RESPONSE_FIELDS = [
15
15
  /** @public */
16
16
  export interface StreamDescriptionOptions {
17
17
  compressors?: CompressorName[];
18
+ logicalSessionTimeoutMinutes?: number;
19
+ loadBalanced: boolean;
18
20
  }
19
21
 
20
22
  /** @public */
@@ -29,6 +31,7 @@ export class StreamDescription {
29
31
  compressors: CompressorName[];
30
32
  compressor?: CompressorName;
31
33
  logicalSessionTimeoutMinutes?: number;
34
+ loadBalanced: boolean;
32
35
 
33
36
  __nodejs_mock_server__?: boolean;
34
37
 
@@ -42,6 +45,8 @@ export class StreamDescription {
42
45
  this.maxBsonObjectSize = 16777216;
43
46
  this.maxMessageSizeBytes = 48000000;
44
47
  this.maxWriteBatchSize = 100000;
48
+ this.logicalSessionTimeoutMinutes = options?.logicalSessionTimeoutMinutes;
49
+ this.loadBalanced = !!options?.loadBalanced;
45
50
  this.compressors =
46
51
  options && options.compressors && Array.isArray(options.compressors)
47
52
  ? options.compressors
@@ -51,7 +56,7 @@ export class StreamDescription {
51
56
  receiveResponse(response: Document): void {
52
57
  this.type = parseServerType(response);
53
58
  RESPONSE_FIELDS.forEach(field => {
54
- if (typeof response[field] !== 'undefined') {
59
+ if (typeof response[field] != null) {
55
60
  this[field] = response[field];
56
61
  }
57
62
 
@@ -2,8 +2,8 @@ import * as zlib from 'zlib';
2
2
  import type { Callback } from '../../utils';
3
3
  import type { OperationDescription } from '../message_stream';
4
4
 
5
- import { Snappy } from '../../deps';
6
- import { MongoDriverError } from '../../error';
5
+ import { PKG_VERSION, Snappy } from '../../deps';
6
+ import { MongoDecompressionError, MongoInvalidArgumentError } from '../../error';
7
7
 
8
8
  /** @public */
9
9
  export const Compressor = Object.freeze({
@@ -39,12 +39,20 @@ export function compress(
39
39
  ): void {
40
40
  const zlibOptions = {} as zlib.ZlibOptions;
41
41
  switch (self.options.agreedCompressor) {
42
- case 'snappy':
42
+ case 'snappy': {
43
43
  if ('kModuleError' in Snappy) {
44
44
  return callback(Snappy['kModuleError']);
45
45
  }
46
- Snappy.compress(dataToBeCompressed, callback);
46
+
47
+ if (Snappy[PKG_VERSION].major <= 6) {
48
+ Snappy.compress(dataToBeCompressed, callback);
49
+ } else {
50
+ Snappy.compress(dataToBeCompressed)
51
+ .then(buffer => callback(undefined, buffer))
52
+ .catch(error => callback(error));
53
+ }
47
54
  break;
55
+ }
48
56
  case 'zlib':
49
57
  // Determine zlibCompressionLevel
50
58
  if (self.options.zlibCompressionLevel) {
@@ -53,10 +61,8 @@ export function compress(
53
61
  zlib.deflate(dataToBeCompressed, zlibOptions, callback as zlib.CompressCallback);
54
62
  break;
55
63
  default:
56
- throw new MongoDriverError(
57
- 'Attempt to compress message using unknown compressor "' +
58
- self.options.agreedCompressor +
59
- '".'
64
+ throw new MongoInvalidArgumentError(
65
+ `Unknown compressor ${self.options.agreedCompressor} failed to compress`
60
66
  );
61
67
  }
62
68
  }
@@ -68,19 +74,26 @@ export function decompress(
68
74
  callback: Callback<Buffer>
69
75
  ): void {
70
76
  if (compressorID < 0 || compressorID > Math.max(2)) {
71
- throw new MongoDriverError(
72
- `Server sent message compressed using an unsupported compressor.` +
73
- ` (Received compressor ID ${compressorID})`
77
+ throw new MongoDecompressionError(
78
+ `Server sent message compressed using an unsupported compressor. (Received compressor ID ${compressorID})`
74
79
  );
75
80
  }
76
81
 
77
82
  switch (compressorID) {
78
- case Compressor.snappy:
83
+ case Compressor.snappy: {
79
84
  if ('kModuleError' in Snappy) {
80
85
  return callback(Snappy['kModuleError']);
81
86
  }
82
- Snappy.uncompress(compressedData, { asBuffer: true }, callback as Callback);
87
+
88
+ if (Snappy[PKG_VERSION].major <= 6) {
89
+ Snappy.uncompress(compressedData, { asBuffer: true }, callback);
90
+ } else {
91
+ Snappy.uncompress(compressedData, { asBuffer: true })
92
+ .then(buffer => callback(undefined, buffer))
93
+ .catch(error => callback(error));
94
+ }
83
95
  break;
96
+ }
84
97
  case Compressor.zlib:
85
98
  zlib.inflate(compressedData, callback as zlib.CompressCallback);
86
99
  break;
@@ -1,6 +1,6 @@
1
1
  import { ServerType } from '../../sdam/common';
2
2
  import { TopologyDescription } from '../../sdam/topology_description';
3
- import { MongoDriverError } from '../../error';
3
+ import { MongoInvalidArgumentError } from '../../error';
4
4
  import { ReadPreference } from '../../read_preference';
5
5
  import type { Document } from '../../bson';
6
6
  import type { OpQueryOptions } from '../commands';
@@ -28,7 +28,9 @@ export function getReadPreference(cmd: Document, options?: ReadPreferenceOption)
28
28
  }
29
29
 
30
30
  if (!(readPreference instanceof ReadPreference)) {
31
- throw new MongoDriverError('read preference must be a ReadPreference instance');
31
+ throw new MongoInvalidArgumentError(
32
+ 'Option "readPreference" must be a ReadPreference instance'
33
+ );
32
34
  }
33
35
 
34
36
  return readPreference;
package/src/collection.ts CHANGED
@@ -8,7 +8,7 @@ import {
8
8
  getTopology
9
9
  } from './utils';
10
10
  import { Document, BSONSerializeOptions, resolveBSONOptions } from './bson';
11
- import { MongoDriverError } from './error';
11
+ import { MongoInvalidArgumentError } from './error';
12
12
  import { UnorderedBulkOperation } from './bulk/unordered';
13
13
  import { OrderedBulkOperation } from './bulk/ordered';
14
14
  import { ChangeStream, ChangeStreamOptions } from './change_stream';
@@ -40,7 +40,6 @@ import {
40
40
  EstimatedDocumentCountOptions
41
41
  } from './operations/estimated_document_count';
42
42
  import type { FindOptions } from './operations/find';
43
- import { FindOneOperation } from './operations/find_one';
44
43
  import {
45
44
  FindOneAndDeleteOperation,
46
45
  FindOneAndReplaceOperation,
@@ -100,7 +99,7 @@ import type {
100
99
 
101
100
  /** @public */
102
101
  export interface ModifyResult<TSchema = Document> {
103
- value?: TSchema;
102
+ value: TSchema | null;
104
103
  lastErrorObject?: Document;
105
104
  ok: 0 | 1;
106
105
  }
@@ -390,7 +389,7 @@ export class Collection<TSchema extends Document = Document> {
390
389
  options = options || { ordered: true };
391
390
 
392
391
  if (!Array.isArray(operations)) {
393
- throw new MongoDriverError('operations must be an array of documents');
392
+ throw new MongoInvalidArgumentError('Argument "operations" must be an array of documents');
394
393
  }
395
394
 
396
395
  return executeOperation(
@@ -415,34 +414,39 @@ export class Collection<TSchema extends Document = Document> {
415
414
  updateOne(
416
415
  filter: Filter<TSchema>,
417
416
  update: UpdateFilter<TSchema> | Partial<TSchema>
418
- ): Promise<UpdateResult | Document>;
417
+ ): Promise<UpdateResult>;
419
418
  updateOne(
420
419
  filter: Filter<TSchema>,
421
420
  update: UpdateFilter<TSchema> | Partial<TSchema>,
422
- callback: Callback<UpdateResult | Document>
421
+ callback: Callback<UpdateResult>
423
422
  ): void;
424
423
  updateOne(
425
424
  filter: Filter<TSchema>,
426
425
  update: UpdateFilter<TSchema> | Partial<TSchema>,
427
426
  options: UpdateOptions
428
- ): Promise<UpdateResult | Document>;
427
+ ): Promise<UpdateResult>;
429
428
  updateOne(
430
429
  filter: Filter<TSchema>,
431
430
  update: UpdateFilter<TSchema> | Partial<TSchema>,
432
431
  options: UpdateOptions,
433
- callback: Callback<UpdateResult | Document>
432
+ callback: Callback<UpdateResult>
434
433
  ): void;
435
434
  updateOne(
436
435
  filter: Filter<TSchema>,
437
436
  update: UpdateFilter<TSchema> | Partial<TSchema>,
438
- options?: UpdateOptions | Callback<UpdateResult | Document>,
439
- callback?: Callback<UpdateResult | Document>
440
- ): Promise<UpdateResult | Document> | void {
437
+ options?: UpdateOptions | Callback<UpdateResult>,
438
+ callback?: Callback<UpdateResult>
439
+ ): Promise<UpdateResult> | void {
441
440
  if (typeof options === 'function') (callback = options), (options = {});
442
441
 
443
442
  return executeOperation(
444
443
  getTopology(this),
445
- new UpdateOneOperation(this as TODO_NODE_3286, filter, update, resolveOptions(this, options)),
444
+ new UpdateOneOperation(
445
+ this as TODO_NODE_3286,
446
+ filter,
447
+ update,
448
+ resolveOptions(this, options)
449
+ ) as TODO_NODE_3286,
446
450
  callback
447
451
  );
448
452
  }
@@ -621,7 +625,7 @@ export class Collection<TSchema extends Document = Document> {
621
625
  */
622
626
  rename(newName: string): Promise<Collection>;
623
627
  rename(newName: string, callback: Callback<Collection>): void;
624
- rename(newName: string, options: RenameOptions): Promise<Collection> | void;
628
+ rename(newName: string, options: RenameOptions): Promise<Collection>;
625
629
  rename(newName: string, options: RenameOptions, callback: Callback<Collection>): void;
626
630
  rename(
627
631
  newName: string,
@@ -672,52 +676,48 @@ export class Collection<TSchema extends Document = Document> {
672
676
  * @param options - Optional settings for the command
673
677
  * @param callback - An optional callback, a Promise will be returned if none is provided
674
678
  */
675
- findOne(): Promise<TSchema | undefined>;
676
- findOne(callback: Callback<TSchema | undefined>): void;
677
- findOne(filter: Filter<TSchema>): Promise<TSchema | undefined>;
678
- findOne(filter: Filter<TSchema>, callback: Callback<TSchema | undefined>): void;
679
- findOne(filter: Filter<TSchema>, options: FindOptions<TSchema>): Promise<TSchema | undefined>;
680
- findOne(
681
- filter: Filter<TSchema>,
682
- options: FindOptions<TSchema>,
683
- callback: Callback<TSchema | undefined>
684
- ): void;
679
+ findOne(): Promise<TSchema | null>;
680
+ findOne(callback: Callback<TSchema | null>): void;
681
+ findOne(filter: Filter<TSchema>): Promise<TSchema | null>;
682
+ findOne(filter: Filter<TSchema>, callback: Callback<TSchema | null>): void;
683
+ findOne(filter: Filter<TSchema>, options: FindOptions): Promise<TSchema | null>;
684
+ findOne(filter: Filter<TSchema>, options: FindOptions, callback: Callback<TSchema | null>): void;
685
685
 
686
686
  // allow an override of the schema.
687
- findOne<T = TSchema>(): Promise<T | undefined>;
688
- findOne<T = TSchema>(callback: Callback<T | undefined>): void;
689
- findOne<T = TSchema>(filter: Filter<T>): Promise<T | undefined>;
690
- findOne<T = TSchema>(filter: Filter<T>, options?: FindOptions<T>): Promise<T | undefined>;
687
+ findOne<T = TSchema>(): Promise<T | null>;
688
+ findOne<T = TSchema>(callback: Callback<T | null>): void;
689
+ findOne<T = TSchema>(filter: Filter<TSchema>): Promise<T | null>;
690
+ findOne<T = TSchema>(filter: Filter<TSchema>, options?: FindOptions): Promise<T | null>;
691
691
  findOne<T = TSchema>(
692
- filter: Filter<T>,
693
- options?: FindOptions<T>,
694
- callback?: Callback<T | undefined>
692
+ filter: Filter<TSchema>,
693
+ options?: FindOptions,
694
+ callback?: Callback<T | null>
695
695
  ): void;
696
696
 
697
697
  findOne(
698
- filter?: Filter<TSchema> | Callback<TSchema | undefined>,
699
- options?: FindOptions<TSchema> | Callback<TSchema | undefined>,
700
- callback?: Callback<TSchema>
701
- ): Promise<TSchema | undefined> | void {
702
- if (callback !== undefined && typeof callback !== 'function') {
703
- throw new MongoDriverError('Third parameter to `findOne()` must be a callback or undefined');
698
+ filter?: Filter<TSchema> | Callback<TSchema | null>,
699
+ options?: FindOptions | Callback<TSchema | null>,
700
+ callback?: Callback<TSchema | null>
701
+ ): Promise<TSchema | null> | void {
702
+ if (callback != null && typeof callback !== 'function') {
703
+ throw new MongoInvalidArgumentError(
704
+ 'Third parameter to `findOne()` must be a callback or undefined'
705
+ );
704
706
  }
705
707
 
706
- if (typeof filter === 'function')
707
- (callback = filter as Callback<Document | undefined>), (filter = {}), (options = {});
708
- if (typeof options === 'function') (callback = options), (options = {});
709
-
710
- filter ??= {};
708
+ if (typeof filter === 'function') {
709
+ callback = filter as Callback<TSchema | null>;
710
+ filter = {};
711
+ options = {};
712
+ }
713
+ if (typeof options === 'function') {
714
+ callback = options;
715
+ options = {};
716
+ }
711
717
 
712
- return executeOperation(
713
- getTopology(this),
714
- new FindOneOperation(
715
- this as TODO_NODE_3286,
716
- filter,
717
- resolveOptions(this, options)
718
- ) as TODO_NODE_3286,
719
- callback as TODO_NODE_3286
720
- );
718
+ const finalFilter = filter ?? {};
719
+ const finalOptions = options ?? {};
720
+ return this.find(finalFilter, finalOptions).limit(-1).batchSize(1).next(callback);
721
721
  }
722
722
 
723
723
  /**
@@ -726,14 +726,16 @@ export class Collection<TSchema extends Document = Document> {
726
726
  * @param filter - The filter predicate. If unspecified, then all documents in the collection will match the predicate
727
727
  */
728
728
  find(): FindCursor<TSchema>;
729
- find(filter: Filter<TSchema>, options?: FindOptions<TSchema>): FindCursor<TSchema>;
730
- find<T = TSchema>(filter: Filter<T>, options?: FindOptions<T>): FindCursor<T>;
731
- find(filter?: Filter<TSchema>, options?: FindOptions<TSchema>): FindCursor<TSchema> {
729
+ find(filter: Filter<TSchema>, options?: FindOptions): FindCursor<TSchema>;
730
+ find<T>(filter: Filter<TSchema>, options?: FindOptions): FindCursor<T>;
731
+ find(filter?: Filter<TSchema>, options?: FindOptions): FindCursor<TSchema> {
732
732
  if (arguments.length > 2) {
733
- throw new MongoDriverError('Third parameter to `collection.find()` must be undefined');
733
+ throw new MongoInvalidArgumentError(
734
+ 'Method "collection.find()" accepts at most two arguments'
735
+ );
734
736
  }
735
737
  if (typeof options === 'function') {
736
- throw new MongoDriverError('`options` parameter must not be function');
738
+ throw new MongoInvalidArgumentError('Argument "options" must not be function');
737
739
  }
738
740
 
739
741
  return new FindCursor<TSchema>(
@@ -1084,7 +1086,7 @@ export class Collection<TSchema extends Document = Document> {
1084
1086
  options?: CountDocumentsOptions | Callback<number>,
1085
1087
  callback?: Callback<number>
1086
1088
  ): Promise<number> | void {
1087
- if (typeof filter === 'undefined') {
1089
+ if (filter == null) {
1088
1090
  (filter = {}), (options = {}), (callback = undefined);
1089
1091
  } else if (typeof filter === 'function') {
1090
1092
  (callback = filter as Callback<number>), (filter = {}), (options = {});
@@ -1188,14 +1190,14 @@ export class Collection<TSchema extends Document = Document> {
1188
1190
  * @param options - Optional settings for the command
1189
1191
  * @param callback - An optional callback, a Promise will be returned if none is provided
1190
1192
  */
1191
- indexes(): Promise<Document>;
1192
- indexes(callback: Callback<Document>): void;
1193
- indexes(options: IndexInformationOptions): Promise<Document>;
1194
- indexes(options: IndexInformationOptions, callback: Callback<Document>): void;
1193
+ indexes(): Promise<Document[]>;
1194
+ indexes(callback: Callback<Document[]>): void;
1195
+ indexes(options: IndexInformationOptions): Promise<Document[]>;
1196
+ indexes(options: IndexInformationOptions, callback: Callback<Document[]>): void;
1195
1197
  indexes(
1196
- options?: IndexInformationOptions | Callback<Document>,
1197
- callback?: Callback<Document>
1198
- ): Promise<Document> | void {
1198
+ options?: IndexInformationOptions | Callback<Document[]>,
1199
+ callback?: Callback<Document[]>
1200
+ ): Promise<Document[]> | void {
1199
1201
  if (typeof options === 'function') (callback = options), (options = {});
1200
1202
 
1201
1203
  return executeOperation(
@@ -1237,11 +1239,11 @@ export class Collection<TSchema extends Document = Document> {
1237
1239
  * @param callback - An optional callback, a Promise will be returned if none is provided
1238
1240
  */
1239
1241
  findOneAndDelete(filter: Filter<TSchema>): Promise<ModifyResult<TSchema>>;
1240
- findOneAndDelete(filter: Filter<TSchema>, callback: Callback<ModifyResult<TSchema>>): void;
1241
1242
  findOneAndDelete(
1242
1243
  filter: Filter<TSchema>,
1243
1244
  options: FindOneAndDeleteOptions
1244
1245
  ): Promise<ModifyResult<TSchema>>;
1246
+ findOneAndDelete(filter: Filter<TSchema>, callback: Callback<ModifyResult<TSchema>>): void;
1245
1247
  findOneAndDelete(
1246
1248
  filter: Filter<TSchema>,
1247
1249
  options: FindOneAndDeleteOptions,
@@ -1369,17 +1371,20 @@ export class Collection<TSchema extends Document = Document> {
1369
1371
  options?: AggregateOptions
1370
1372
  ): AggregationCursor<T> {
1371
1373
  if (arguments.length > 2) {
1372
- throw new MongoDriverError('Third parameter to `collection.aggregate()` must be undefined');
1374
+ throw new MongoInvalidArgumentError(
1375
+ 'Method "collection.aggregate()" accepts at most two arguments'
1376
+ );
1373
1377
  }
1374
1378
  if (!Array.isArray(pipeline)) {
1375
- throw new MongoDriverError('`pipeline` parameter must be an array of aggregation stages');
1379
+ throw new MongoInvalidArgumentError(
1380
+ 'Argument "pipeline" must be an array of aggregation stages'
1381
+ );
1376
1382
  }
1377
1383
  if (typeof options === 'function') {
1378
- throw new MongoDriverError('`options` parameter must not be function');
1384
+ throw new MongoInvalidArgumentError('Argument "options" must not be function');
1379
1385
  }
1380
1386
 
1381
1387
  return new AggregationCursor(
1382
- this as TODO_NODE_3286,
1383
1388
  getTopology(this),
1384
1389
  this.s.namespace,
1385
1390
  pipeline,
@@ -1445,8 +1450,8 @@ export class Collection<TSchema extends Document = Document> {
1445
1450
  // Out must always be defined (make sure we don't break weirdly on pre 1.8+ servers)
1446
1451
  // TODO NODE-3339: Figure out if this is still necessary given we no longer officially support pre-1.8
1447
1452
  if (options?.out == null) {
1448
- throw new MongoDriverError(
1449
- 'the out option parameter must be defined, see mongodb docs for possible values'
1453
+ throw new MongoInvalidArgumentError(
1454
+ 'Option "out" must be defined, see mongodb docs for possible values'
1450
1455
  );
1451
1456
  }
1452
1457