mongodb 6.9.0-dev.20240918.sha.643a8755 → 6.9.0-dev.20240927.sha.681ddd8d

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 (37) hide show
  1. package/lib/beta.d.ts +243 -0
  2. package/lib/cmap/command_monitoring_events.js +10 -1
  3. package/lib/cmap/command_monitoring_events.js.map +1 -1
  4. package/lib/cmap/commands.js +4 -4
  5. package/lib/cmap/commands.js.map +1 -1
  6. package/lib/cmap/wire_protocol/responses.js +23 -1
  7. package/lib/cmap/wire_protocol/responses.js.map +1 -1
  8. package/lib/cursor/client_bulk_write_cursor.js +50 -0
  9. package/lib/cursor/client_bulk_write_cursor.js.map +1 -0
  10. package/lib/error.js +27 -1
  11. package/lib/error.js.map +1 -1
  12. package/lib/index.js +4 -3
  13. package/lib/index.js.map +1 -1
  14. package/lib/mongo_client.js +10 -0
  15. package/lib/mongo_client.js.map +1 -1
  16. package/lib/operations/client_bulk_write/client_bulk_write.js +35 -0
  17. package/lib/operations/client_bulk_write/client_bulk_write.js.map +1 -0
  18. package/lib/operations/client_bulk_write/command_builder.js +20 -6
  19. package/lib/operations/client_bulk_write/command_builder.js.map +1 -1
  20. package/lib/operations/client_bulk_write/executor.js +71 -0
  21. package/lib/operations/client_bulk_write/executor.js.map +1 -0
  22. package/lib/operations/client_bulk_write/results_merger.js +79 -0
  23. package/lib/operations/client_bulk_write/results_merger.js.map +1 -0
  24. package/mongodb.d.ts +243 -0
  25. package/package.json +1 -1
  26. package/src/cmap/command_monitoring_events.ts +16 -2
  27. package/src/cmap/commands.ts +4 -4
  28. package/src/cmap/wire_protocol/responses.ts +26 -0
  29. package/src/cursor/client_bulk_write_cursor.ts +73 -0
  30. package/src/error.ts +27 -0
  31. package/src/index.ts +16 -0
  32. package/src/mongo_client.ts +19 -0
  33. package/src/operations/client_bulk_write/client_bulk_write.ts +45 -0
  34. package/src/operations/client_bulk_write/command_builder.ts +35 -6
  35. package/src/operations/client_bulk_write/common.ts +79 -0
  36. package/src/operations/client_bulk_write/executor.ts +99 -0
  37. package/src/operations/client_bulk_write/results_merger.ts +95 -0
package/mongodb.d.ts CHANGED
@@ -514,6 +514,13 @@ export declare type AnyBulkWriteOperation<TSchema extends Document = Document> =
514
514
  deleteMany: DeleteManyModel<TSchema>;
515
515
  };
516
516
 
517
+ /**
518
+ * Used to represent any of the client bulk write models that can be passed as an array
519
+ * to MongoClient#bulkWrite.
520
+ * @public
521
+ */
522
+ export declare type AnyClientBulkWriteModel = ClientInsertOneModel | ClientReplaceOneModel | ClientUpdateOneModel | ClientUpdateManyModel | ClientDeleteOneModel | ClientDeleteManyModel;
523
+
517
524
  /** @public */
518
525
  export declare type AnyError = MongoError | Error;
519
526
 
@@ -1480,6 +1487,100 @@ export declare interface ChangeStreamUpdateDocument<TSchema extends Document = D
1480
1487
  fullDocumentBeforeChange?: TSchema;
1481
1488
  }
1482
1489
 
1490
+ /** @public */
1491
+ export declare interface ClientBulkWriteOptions extends CommandOperationOptions {
1492
+ /**
1493
+ * If true, when an insert fails, don't execute the remaining writes.
1494
+ * If false, continue with remaining inserts when one fails.
1495
+ * @defaultValue `true` - inserts are ordered by default
1496
+ */
1497
+ ordered?: boolean;
1498
+ /**
1499
+ * Allow driver to bypass schema validation.
1500
+ * @defaultValue `false` - documents will be validated by default
1501
+ **/
1502
+ bypassDocumentValidation?: boolean;
1503
+ /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */
1504
+ let?: Document;
1505
+ /**
1506
+ * Whether detailed results for each successful operation should be included in the returned
1507
+ * BulkWriteResult.
1508
+ */
1509
+ verboseResults?: boolean;
1510
+ }
1511
+
1512
+ /** @public */
1513
+ export declare interface ClientBulkWriteResult {
1514
+ /**
1515
+ * The total number of documents inserted across all insert operations.
1516
+ */
1517
+ insertedCount: number;
1518
+ /**
1519
+ * The total number of documents upserted across all update operations.
1520
+ */
1521
+ upsertedCount: number;
1522
+ /**
1523
+ * The total number of documents matched across all update operations.
1524
+ */
1525
+ matchedCount: number;
1526
+ /**
1527
+ * The total number of documents modified across all update operations.
1528
+ */
1529
+ modifiedCount: number;
1530
+ /**
1531
+ * The total number of documents deleted across all delete operations.
1532
+ */
1533
+ deletedCount: number;
1534
+ /**
1535
+ * The results of each individual insert operation that was successfully performed.
1536
+ */
1537
+ insertResults?: Map<number, ClientInsertOneResult>;
1538
+ /**
1539
+ * The results of each individual update operation that was successfully performed.
1540
+ */
1541
+ updateResults?: Map<number, ClientUpdateResult>;
1542
+ /**
1543
+ * The results of each individual delete operation that was successfully performed.
1544
+ */
1545
+ deleteResults?: Map<number, ClientDeleteResult>;
1546
+ }
1547
+
1548
+ /** @public */
1549
+ export declare interface ClientDeleteManyModel extends ClientWriteModel {
1550
+ name: 'deleteMany';
1551
+ /**
1552
+ * The filter used to determine if a document should be deleted.
1553
+ * For a deleteMany operation, all matches are removed.
1554
+ */
1555
+ filter: Filter<Document>;
1556
+ /** Specifies a collation. */
1557
+ collation?: CollationOptions;
1558
+ /** The index to use. If specified, then the query system will only consider plans using the hinted index. */
1559
+ hint?: Hint;
1560
+ }
1561
+
1562
+ /** @public */
1563
+ export declare interface ClientDeleteOneModel extends ClientWriteModel {
1564
+ name: 'deleteOne';
1565
+ /**
1566
+ * The filter used to determine if a document should be deleted.
1567
+ * For a deleteOne operation, the first match is removed.
1568
+ */
1569
+ filter: Filter<Document>;
1570
+ /** Specifies a collation. */
1571
+ collation?: CollationOptions;
1572
+ /** The index to use. If specified, then the query system will only consider plans using the hinted index. */
1573
+ hint?: Hint;
1574
+ }
1575
+
1576
+ /** @public */
1577
+ export declare interface ClientDeleteResult {
1578
+ /**
1579
+ * The number of documents that were deleted.
1580
+ */
1581
+ deletedCount: number;
1582
+ }
1583
+
1483
1584
  /**
1484
1585
  * @public
1485
1586
  * The public interface for explicit in-use encryption
@@ -1901,6 +2002,21 @@ export declare type ClientEncryptionSocketOptions = Pick<MongoClientOptions, 'au
1901
2002
  */
1902
2003
  export declare type ClientEncryptionTlsOptions = Pick<MongoClientOptions, 'tlsCAFile' | 'tlsCertificateKeyFile' | 'tlsCertificateKeyFilePassword'>;
1903
2004
 
2005
+ /** @public */
2006
+ export declare interface ClientInsertOneModel extends ClientWriteModel {
2007
+ name: 'insertOne';
2008
+ /** The document to insert. */
2009
+ document: OptionalId<Document>;
2010
+ }
2011
+
2012
+ /** @public */
2013
+ export declare interface ClientInsertOneResult {
2014
+ /**
2015
+ * The _id of the inserted document.
2016
+ */
2017
+ insertedId: any;
2018
+ }
2019
+
1904
2020
  /**
1905
2021
  * @public
1906
2022
  * @see https://github.com/mongodb/specifications/blob/master/source/mongodb-handshake/handshake.rst#hello-command
@@ -1940,6 +2056,24 @@ export declare interface ClientMetadataOptions {
1940
2056
  appName?: string;
1941
2057
  }
1942
2058
 
2059
+ /** @public */
2060
+ export declare interface ClientReplaceOneModel extends ClientWriteModel {
2061
+ name: 'replaceOne';
2062
+ /**
2063
+ * The filter used to determine if a document should be replaced.
2064
+ * For a replaceOne operation, the first match is replaced.
2065
+ */
2066
+ filter: Filter<Document>;
2067
+ /** The document with which to replace the matched document. */
2068
+ replacement: WithoutId<Document>;
2069
+ /** Specifies a collation. */
2070
+ collation?: CollationOptions;
2071
+ /** The index to use. If specified, then the query system will only consider plans using the hinted index. */
2072
+ hint?: Hint;
2073
+ /** When true, creates a new document if no document matches the query. */
2074
+ upsert?: boolean;
2075
+ }
2076
+
1943
2077
  /**
1944
2078
  * A class representing a client session on the server
1945
2079
  *
@@ -2094,6 +2228,84 @@ export declare interface ClientSessionOptions {
2094
2228
  /* Excluded from this release type: initialClusterTime */
2095
2229
  }
2096
2230
 
2231
+ /** @public */
2232
+ export declare interface ClientUpdateManyModel extends ClientWriteModel {
2233
+ name: 'updateMany';
2234
+ /**
2235
+ * The filter used to determine if a document should be updated.
2236
+ * For an updateMany operation, all matches are updated.
2237
+ */
2238
+ filter: Filter<Document>;
2239
+ /**
2240
+ * The modifications to apply. The value can be either:
2241
+ * UpdateFilter<Document> - A document that contains update operator expressions,
2242
+ * Document[] - an aggregation pipeline.
2243
+ */
2244
+ update: UpdateFilter<Document> | Document[];
2245
+ /** A set of filters specifying to which array elements an update should apply. */
2246
+ arrayFilters?: Document[];
2247
+ /** Specifies a collation. */
2248
+ collation?: CollationOptions;
2249
+ /** The index to use. If specified, then the query system will only consider plans using the hinted index. */
2250
+ hint?: Hint;
2251
+ /** When true, creates a new document if no document matches the query. */
2252
+ upsert?: boolean;
2253
+ }
2254
+
2255
+ /** @public */
2256
+ export declare interface ClientUpdateOneModel extends ClientWriteModel {
2257
+ name: 'updateOne';
2258
+ /**
2259
+ * The filter used to determine if a document should be updated.
2260
+ * For an updateOne operation, the first match is updated.
2261
+ */
2262
+ filter: Filter<Document>;
2263
+ /**
2264
+ * The modifications to apply. The value can be either:
2265
+ * UpdateFilter<Document> - A document that contains update operator expressions,
2266
+ * Document[] - an aggregation pipeline.
2267
+ */
2268
+ update: UpdateFilter<Document> | Document[];
2269
+ /** A set of filters specifying to which array elements an update should apply. */
2270
+ arrayFilters?: Document[];
2271
+ /** Specifies a collation. */
2272
+ collation?: CollationOptions;
2273
+ /** The index to use. If specified, then the query system will only consider plans using the hinted index. */
2274
+ hint?: Hint;
2275
+ /** When true, creates a new document if no document matches the query. */
2276
+ upsert?: boolean;
2277
+ }
2278
+
2279
+ /** @public */
2280
+ export declare interface ClientUpdateResult {
2281
+ /**
2282
+ * The number of documents that matched the filter.
2283
+ */
2284
+ matchedCount: number;
2285
+ /**
2286
+ * The number of documents that were modified.
2287
+ */
2288
+ modifiedCount: number;
2289
+ /**
2290
+ * The _id field of the upserted document if an upsert occurred.
2291
+ *
2292
+ * It MUST be possible to discern between a BSON Null upserted ID value and this field being
2293
+ * unset. If necessary, drivers MAY add a didUpsert boolean field to differentiate between
2294
+ * these two cases.
2295
+ */
2296
+ upsertedId?: any;
2297
+ /**
2298
+ * Determines if the upsert did include an _id, which includes the case of the _id being null.
2299
+ */
2300
+ didUpsert: boolean;
2301
+ }
2302
+
2303
+ /** @public */
2304
+ export declare interface ClientWriteModel {
2305
+ /** The namespace for the write. */
2306
+ namespace: string;
2307
+ }
2308
+
2097
2309
  /**
2098
2310
  * @public
2099
2311
  * @deprecated This interface is deprecated and will be removed in a future release as it is not used
@@ -4894,6 +5106,28 @@ export declare class MongoBatchReExecutionError extends MongoAPIError {
4894
5106
  get name(): string;
4895
5107
  }
4896
5108
 
5109
+ /**
5110
+ * An error indicating that an error occurred when processing bulk write results.
5111
+ *
5112
+ * @public
5113
+ * @category Error
5114
+ */
5115
+ export declare class MongoBulkWriteCursorError extends MongoRuntimeError {
5116
+ /**
5117
+ * **Do not use this constructor!**
5118
+ *
5119
+ * Meant for internal use only.
5120
+ *
5121
+ * @remarks
5122
+ * This class is only meant to be constructed within the driver. This constructor is
5123
+ * not subject to semantic versioning compatibility guarantees and may change at any time.
5124
+ *
5125
+ * @public
5126
+ **/
5127
+ constructor(message: string);
5128
+ get name(): string;
5129
+ }
5130
+
4897
5131
  /**
4898
5132
  * An error indicating an unsuccessful Bulk Write
4899
5133
  * @public
@@ -5001,6 +5235,15 @@ export declare class MongoClient extends TypedEventEmitter<MongoClientEvents> im
5001
5235
  get writeConcern(): WriteConcern | undefined;
5002
5236
  get readPreference(): ReadPreference;
5003
5237
  get bsonOptions(): BSONSerializeOptions;
5238
+ /**
5239
+ * Executes a client bulk write operation, available on server 8.0+.
5240
+ * @param models - The client bulk write models.
5241
+ * @param options - The client bulk write options.
5242
+ * @returns A ClientBulkWriteResult for acknowledged writes and ok: 1 for unacknowledged writes.
5243
+ */
5244
+ bulkWrite(models: AnyClientBulkWriteModel[], options?: ClientBulkWriteOptions): Promise<ClientBulkWriteResult | {
5245
+ ok: 1;
5246
+ }>;
5004
5247
  /**
5005
5248
  * Connect to MongoDB using a url
5006
5249
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mongodb",
3
- "version": "6.9.0-dev.20240918.sha.643a8755",
3
+ "version": "6.9.0-dev.20240927.sha.681ddd8d",
4
4
  "description": "The official MongoDB driver for Node.js",
5
5
  "main": "lib/index.js",
6
6
  "files": [
@@ -7,7 +7,12 @@ import {
7
7
  LEGACY_HELLO_COMMAND_CAMEL_CASE
8
8
  } from '../constants';
9
9
  import { calculateDurationInMs, deepCopy } from '../utils';
10
- import { OpMsgRequest, type OpQueryRequest, type WriteProtocolMessageType } from './commands';
10
+ import {
11
+ DocumentSequence,
12
+ OpMsgRequest,
13
+ type OpQueryRequest,
14
+ type WriteProtocolMessageType
15
+ } from './commands';
11
16
  import type { Connection } from './connection';
12
17
 
13
18
  /**
@@ -249,7 +254,16 @@ const OP_QUERY_KEYS = [
249
254
  /** Extract the actual command from the query, possibly up-converting if it's a legacy format */
250
255
  function extractCommand(command: WriteProtocolMessageType): Document {
251
256
  if (command instanceof OpMsgRequest) {
252
- return deepCopy(command.command);
257
+ const cmd = deepCopy(command.command);
258
+ // For OP_MSG with payload type 1 we need to pull the documents
259
+ // array out of the document sequence for monitoring.
260
+ if (cmd.ops instanceof DocumentSequence) {
261
+ cmd.ops = cmd.ops.documents;
262
+ }
263
+ if (cmd.nsInfo instanceof DocumentSequence) {
264
+ cmd.nsInfo = cmd.nsInfo.documents;
265
+ }
266
+ return cmd;
253
267
  }
254
268
 
255
269
  if (command.query?.$query) {
@@ -544,10 +544,10 @@ export class OpMsgRequest {
544
544
  for (const [key, value] of Object.entries(document)) {
545
545
  if (value instanceof DocumentSequence) {
546
546
  // Document sequences starts with type 1 at the first byte.
547
- const buffer = Buffer.allocUnsafe(1 + 4 + key.length);
547
+ const buffer = Buffer.allocUnsafe(1 + 4 + key.length + 1);
548
548
  buffer[0] = 1;
549
- // Third part is the field name at offset 5.
550
- encodeUTF8Into(buffer, key, 5);
549
+ // Third part is the field name at offset 5 with trailing null byte.
550
+ encodeUTF8Into(buffer, `${key}\0`, 5);
551
551
  chunks.push(buffer);
552
552
  // Fourth part are the documents' bytes.
553
553
  let docsLength = 0;
@@ -557,7 +557,7 @@ export class OpMsgRequest {
557
557
  chunks.push(docBson);
558
558
  }
559
559
  // Second part of the sequence is the length at offset 1;
560
- buffer.writeInt32LE(key.length + docsLength, 1);
560
+ buffer.writeInt32LE(4 + key.length + 1 + docsLength, 1);
561
561
  // Why are we removing the field from the command? This is because it needs to be
562
562
  // removed in the OP_MSG request first section, and DocumentSequence is not a
563
563
  // BSON type and is specific to the MongoDB wire protocol so there's nothing
@@ -329,3 +329,29 @@ export class ExplainedCursorResponse extends CursorResponse {
329
329
  return this.toObject(options);
330
330
  }
331
331
  }
332
+
333
+ /**
334
+ * Client bulk writes have some extra metadata at the top level that needs to be
335
+ * included in the result returned to the user.
336
+ */
337
+ export class ClientBulkWriteCursorResponse extends CursorResponse {
338
+ get insertedCount() {
339
+ return this.get('nInserted', BSONType.int, true);
340
+ }
341
+
342
+ get upsertedCount() {
343
+ return this.get('nUpserted', BSONType.int, true);
344
+ }
345
+
346
+ get matchedCount() {
347
+ return this.get('nMatched', BSONType.int, true);
348
+ }
349
+
350
+ get modifiedCount() {
351
+ return this.get('nModified', BSONType.int, true);
352
+ }
353
+
354
+ get deletedCount() {
355
+ return this.get('nDeleted', BSONType.int, true);
356
+ }
357
+ }
@@ -0,0 +1,73 @@
1
+ import type { Document } from '../bson';
2
+ import { type ClientBulkWriteCursorResponse } from '../cmap/wire_protocol/responses';
3
+ import { MongoBulkWriteCursorError } from '../error';
4
+ import type { MongoClient } from '../mongo_client';
5
+ import { ClientBulkWriteOperation } from '../operations/client_bulk_write/client_bulk_write';
6
+ import { type ClientBulkWriteOptions } from '../operations/client_bulk_write/common';
7
+ import { executeOperation } from '../operations/execute_operation';
8
+ import type { ClientSession } from '../sessions';
9
+ import { mergeOptions, MongoDBNamespace } from '../utils';
10
+ import {
11
+ AbstractCursor,
12
+ type AbstractCursorOptions,
13
+ type InitialCursorResponse
14
+ } from './abstract_cursor';
15
+
16
+ /** @public */
17
+ export interface ClientBulkWriteCursorOptions
18
+ extends Omit<AbstractCursorOptions, 'maxAwaitTimeMS' | 'tailable' | 'awaitData'>,
19
+ ClientBulkWriteOptions {}
20
+
21
+ /**
22
+ * This is the cursor that handles client bulk write operations. Note this is never
23
+ * exposed directly to the user and is always immediately exhausted.
24
+ * @internal
25
+ */
26
+ export class ClientBulkWriteCursor extends AbstractCursor {
27
+ public readonly command: Document;
28
+ /** @internal */
29
+ private cursorResponse?: ClientBulkWriteCursorResponse;
30
+ /** @internal */
31
+ private clientBulkWriteOptions: ClientBulkWriteOptions;
32
+
33
+ /** @internal */
34
+ constructor(client: MongoClient, command: Document, options: ClientBulkWriteOptions = {}) {
35
+ super(client, new MongoDBNamespace('admin', '$cmd'), options);
36
+
37
+ this.command = command;
38
+ this.clientBulkWriteOptions = options;
39
+ }
40
+
41
+ /**
42
+ * We need a way to get the top level cursor response fields for
43
+ * generating the bulk write result, so we expose this here.
44
+ */
45
+ get response(): ClientBulkWriteCursorResponse {
46
+ if (this.cursorResponse) return this.cursorResponse;
47
+ throw new MongoBulkWriteCursorError(
48
+ 'No client bulk write cursor response returned from the server.'
49
+ );
50
+ }
51
+
52
+ clone(): ClientBulkWriteCursor {
53
+ const clonedOptions = mergeOptions({}, this.clientBulkWriteOptions);
54
+ delete clonedOptions.session;
55
+ return new ClientBulkWriteCursor(this.client, this.command, {
56
+ ...clonedOptions
57
+ });
58
+ }
59
+
60
+ /** @internal */
61
+ async _initialize(session: ClientSession): Promise<InitialCursorResponse> {
62
+ const clientBulkWriteOperation = new ClientBulkWriteOperation(this.command, {
63
+ ...this.clientBulkWriteOptions,
64
+ ...this.cursorOptions,
65
+ session
66
+ });
67
+
68
+ const response = await executeOperation(this.client, clientBulkWriteOperation);
69
+ this.cursorResponse = response;
70
+
71
+ return { server: clientBulkWriteOperation.server, session, response };
72
+ }
73
+ }
package/src/error.ts CHANGED
@@ -616,6 +616,33 @@ export class MongoGCPError extends MongoOIDCError {
616
616
  }
617
617
  }
618
618
 
619
+ /**
620
+ * An error indicating that an error occurred when processing bulk write results.
621
+ *
622
+ * @public
623
+ * @category Error
624
+ */
625
+ export class MongoBulkWriteCursorError extends MongoRuntimeError {
626
+ /**
627
+ * **Do not use this constructor!**
628
+ *
629
+ * Meant for internal use only.
630
+ *
631
+ * @remarks
632
+ * This class is only meant to be constructed within the driver. This constructor is
633
+ * not subject to semantic versioning compatibility guarantees and may change at any time.
634
+ *
635
+ * @public
636
+ **/
637
+ constructor(message: string) {
638
+ super(message);
639
+ }
640
+
641
+ override get name(): string {
642
+ return 'MongoBulkWriteCursorError';
643
+ }
644
+ }
645
+
619
646
  /**
620
647
  * An error generated when a ChangeStream operation fails to execute.
621
648
  *
package/src/index.ts CHANGED
@@ -44,6 +44,7 @@ export {
44
44
  MongoAWSError,
45
45
  MongoAzureError,
46
46
  MongoBatchReExecutionError,
47
+ MongoBulkWriteCursorError,
47
48
  MongoChangeStreamError,
48
49
  MongoCompatibilityError,
49
50
  MongoCursorExhaustedError,
@@ -473,6 +474,21 @@ export type {
473
474
  AggregateOptions,
474
475
  DB_AGGREGATE_COLLECTION
475
476
  } from './operations/aggregate';
477
+ export type {
478
+ AnyClientBulkWriteModel,
479
+ ClientBulkWriteOptions,
480
+ ClientBulkWriteResult,
481
+ ClientDeleteManyModel,
482
+ ClientDeleteOneModel,
483
+ ClientDeleteResult,
484
+ ClientInsertOneModel,
485
+ ClientInsertOneResult,
486
+ ClientReplaceOneModel,
487
+ ClientUpdateManyModel,
488
+ ClientUpdateOneModel,
489
+ ClientUpdateResult,
490
+ ClientWriteModel
491
+ } from './operations/client_bulk_write/common';
476
492
  export type {
477
493
  CollationOptions,
478
494
  CommandOperation,
@@ -30,6 +30,12 @@ import {
30
30
  SeverityLevel
31
31
  } from './mongo_logger';
32
32
  import { TypedEventEmitter } from './mongo_types';
33
+ import {
34
+ type AnyClientBulkWriteModel,
35
+ type ClientBulkWriteOptions,
36
+ type ClientBulkWriteResult
37
+ } from './operations/client_bulk_write/common';
38
+ import { ClientBulkWriteExecutor } from './operations/client_bulk_write/executor';
33
39
  import { executeOperation } from './operations/execute_operation';
34
40
  import { RunAdminCommandOperation } from './operations/run_command';
35
41
  import type { ReadConcern, ReadConcernLevel, ReadConcernLike } from './read_concern';
@@ -477,6 +483,19 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
477
483
  return this.s.bsonOptions;
478
484
  }
479
485
 
486
+ /**
487
+ * Executes a client bulk write operation, available on server 8.0+.
488
+ * @param models - The client bulk write models.
489
+ * @param options - The client bulk write options.
490
+ * @returns A ClientBulkWriteResult for acknowledged writes and ok: 1 for unacknowledged writes.
491
+ */
492
+ async bulkWrite(
493
+ models: AnyClientBulkWriteModel[],
494
+ options?: ClientBulkWriteOptions
495
+ ): Promise<ClientBulkWriteResult | { ok: 1 }> {
496
+ return await new ClientBulkWriteExecutor(this, models, options).execute();
497
+ }
498
+
480
499
  /**
481
500
  * Connect to MongoDB using a url
482
501
  *
@@ -0,0 +1,45 @@
1
+ import { type Document } from 'bson';
2
+
3
+ import { ClientBulkWriteCursorResponse } from '../../cmap/wire_protocol/responses';
4
+ import type { Server } from '../../sdam/server';
5
+ import type { ClientSession } from '../../sessions';
6
+ import { MongoDBNamespace } from '../../utils';
7
+ import { CommandOperation } from '../command';
8
+ import { Aspect, defineAspects } from '../operation';
9
+ import { type ClientBulkWriteOptions } from './common';
10
+
11
+ /**
12
+ * Executes a single client bulk write operation within a potential batch.
13
+ * @internal
14
+ */
15
+ export class ClientBulkWriteOperation extends CommandOperation<ClientBulkWriteCursorResponse> {
16
+ command: Document;
17
+ override options: ClientBulkWriteOptions;
18
+
19
+ override get commandName() {
20
+ return 'bulkWrite' as const;
21
+ }
22
+
23
+ constructor(command: Document, options: ClientBulkWriteOptions) {
24
+ super(undefined, options);
25
+ this.command = command;
26
+ this.options = options;
27
+ this.ns = new MongoDBNamespace('admin', '$cmd');
28
+ }
29
+
30
+ /**
31
+ * Execute the command. Superclass will handle write concern, etc.
32
+ * @param server - The server.
33
+ * @param session - The session.
34
+ * @returns The response.
35
+ */
36
+ override async execute(
37
+ server: Server,
38
+ session: ClientSession | undefined
39
+ ): Promise<ClientBulkWriteCursorResponse> {
40
+ return await super.executeCommand(server, session, this.command, ClientBulkWriteCursorResponse);
41
+ }
42
+ }
43
+
44
+ // Skipping the collation as it goes on the individual ops.
45
+ defineAspects(ClientBulkWriteOperation, [Aspect.WRITE_OPERATION, Aspect.SKIP_COLLATION]);