mongodb 6.8.0-dev.20240824.sha.40ace73c → 6.8.0-dev.20240829.sha.6d65ae77

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.
@@ -0,0 +1,198 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildReplaceOneOperation = exports.buildUpdateManyOperation = exports.buildUpdateOneOperation = exports.buildDeleteManyOperation = exports.buildDeleteOneOperation = exports.buildInsertOneOperation = exports.ClientBulkWriteCommandBuilder = void 0;
4
+ exports.buildOperation = buildOperation;
5
+ const commands_1 = require("../../cmap/commands");
6
+ /** @internal */
7
+ class ClientBulkWriteCommandBuilder {
8
+ /**
9
+ * Create the command builder.
10
+ * @param models - The client write models.
11
+ */
12
+ constructor(models, options) {
13
+ this.models = models;
14
+ this.options = options;
15
+ }
16
+ /**
17
+ * Gets the errorsOnly value for the command, which is the inverse of the
18
+ * user provided verboseResults option. Defaults to true.
19
+ */
20
+ get errorsOnly() {
21
+ if ('verboseResults' in this.options) {
22
+ return !this.options.verboseResults;
23
+ }
24
+ return true;
25
+ }
26
+ /**
27
+ * Build the bulk write commands from the models.
28
+ */
29
+ buildCommands() {
30
+ // Iterate the models to build the ops and nsInfo fields.
31
+ const operations = [];
32
+ let currentNamespaceIndex = 0;
33
+ const namespaces = new Map();
34
+ for (const model of this.models) {
35
+ const ns = model.namespace;
36
+ const index = namespaces.get(ns);
37
+ if (index != null) {
38
+ operations.push(buildOperation(model, index));
39
+ }
40
+ else {
41
+ namespaces.set(ns, currentNamespaceIndex);
42
+ operations.push(buildOperation(model, currentNamespaceIndex));
43
+ currentNamespaceIndex++;
44
+ }
45
+ }
46
+ const nsInfo = Array.from(namespaces.keys(), ns => ({ ns }));
47
+ // The base command.
48
+ const command = {
49
+ bulkWrite: 1,
50
+ errorsOnly: this.errorsOnly,
51
+ ordered: this.options.ordered ?? true,
52
+ ops: new commands_1.DocumentSequence(operations),
53
+ nsInfo: new commands_1.DocumentSequence(nsInfo)
54
+ };
55
+ // Add bypassDocumentValidation if it was present in the options.
56
+ if (this.options.bypassDocumentValidation != null) {
57
+ command.bypassDocumentValidation = this.options.bypassDocumentValidation;
58
+ }
59
+ // Add let if it was present in the options.
60
+ if (this.options.let) {
61
+ command.let = this.options.let;
62
+ }
63
+ return [command];
64
+ }
65
+ }
66
+ exports.ClientBulkWriteCommandBuilder = ClientBulkWriteCommandBuilder;
67
+ /**
68
+ * Build the insert one operation.
69
+ * @param model - The insert one model.
70
+ * @param index - The namespace index.
71
+ * @returns the operation.
72
+ */
73
+ const buildInsertOneOperation = (model, index) => {
74
+ const document = {
75
+ insert: index,
76
+ document: model.document
77
+ };
78
+ return document;
79
+ };
80
+ exports.buildInsertOneOperation = buildInsertOneOperation;
81
+ /**
82
+ * Build the delete one operation.
83
+ * @param model - The insert many model.
84
+ * @param index - The namespace index.
85
+ * @returns the operation.
86
+ */
87
+ const buildDeleteOneOperation = (model, index) => {
88
+ return createDeleteOperation(model, index, false);
89
+ };
90
+ exports.buildDeleteOneOperation = buildDeleteOneOperation;
91
+ /**
92
+ * Build the delete many operation.
93
+ * @param model - The delete many model.
94
+ * @param index - The namespace index.
95
+ * @returns the operation.
96
+ */
97
+ const buildDeleteManyOperation = (model, index) => {
98
+ return createDeleteOperation(model, index, true);
99
+ };
100
+ exports.buildDeleteManyOperation = buildDeleteManyOperation;
101
+ /**
102
+ * Creates a delete operation based on the parameters.
103
+ */
104
+ function createDeleteOperation(model, index, multi) {
105
+ const document = {
106
+ delete: index,
107
+ multi: multi,
108
+ filter: model.filter
109
+ };
110
+ if (model.hint) {
111
+ document.hint = model.hint;
112
+ }
113
+ if (model.collation) {
114
+ document.collation = model.collation;
115
+ }
116
+ return document;
117
+ }
118
+ /**
119
+ * Build the update one operation.
120
+ * @param model - The update one model.
121
+ * @param index - The namespace index.
122
+ * @returns the operation.
123
+ */
124
+ const buildUpdateOneOperation = (model, index) => {
125
+ return createUpdateOperation(model, index, false);
126
+ };
127
+ exports.buildUpdateOneOperation = buildUpdateOneOperation;
128
+ /**
129
+ * Build the update many operation.
130
+ * @param model - The update many model.
131
+ * @param index - The namespace index.
132
+ * @returns the operation.
133
+ */
134
+ const buildUpdateManyOperation = (model, index) => {
135
+ return createUpdateOperation(model, index, true);
136
+ };
137
+ exports.buildUpdateManyOperation = buildUpdateManyOperation;
138
+ /**
139
+ * Creates a delete operation based on the parameters.
140
+ */
141
+ function createUpdateOperation(model, index, multi) {
142
+ const document = {
143
+ update: index,
144
+ multi: multi,
145
+ filter: model.filter,
146
+ updateMods: model.update
147
+ };
148
+ if (model.hint) {
149
+ document.hint = model.hint;
150
+ }
151
+ if (model.upsert) {
152
+ document.upsert = model.upsert;
153
+ }
154
+ if (model.arrayFilters) {
155
+ document.arrayFilters = model.arrayFilters;
156
+ }
157
+ return document;
158
+ }
159
+ /**
160
+ * Build the replace one operation.
161
+ * @param model - The replace one model.
162
+ * @param index - The namespace index.
163
+ * @returns the operation.
164
+ */
165
+ const buildReplaceOneOperation = (model, index) => {
166
+ const document = {
167
+ update: index,
168
+ multi: false,
169
+ filter: model.filter,
170
+ updateMods: model.replacement
171
+ };
172
+ if (model.hint) {
173
+ document.hint = model.hint;
174
+ }
175
+ if (model.upsert) {
176
+ document.upsert = model.upsert;
177
+ }
178
+ return document;
179
+ };
180
+ exports.buildReplaceOneOperation = buildReplaceOneOperation;
181
+ /** @internal */
182
+ function buildOperation(model, index) {
183
+ switch (model.name) {
184
+ case 'insertOne':
185
+ return (0, exports.buildInsertOneOperation)(model, index);
186
+ case 'deleteOne':
187
+ return (0, exports.buildDeleteOneOperation)(model, index);
188
+ case 'deleteMany':
189
+ return (0, exports.buildDeleteManyOperation)(model, index);
190
+ case 'updateOne':
191
+ return (0, exports.buildUpdateOneOperation)(model, index);
192
+ case 'updateMany':
193
+ return (0, exports.buildUpdateManyOperation)(model, index);
194
+ case 'replaceOne':
195
+ return (0, exports.buildReplaceOneOperation)(model, index);
196
+ }
197
+ }
198
+ //# sourceMappingURL=command_builder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"command_builder.js","sourceRoot":"","sources":["../../../src/operations/client_bulk_write/command_builder.ts"],"names":[],"mappings":";;;AA2QA,wCAeC;AAzRD,kDAAuD;AA0BvD,gBAAgB;AAChB,MAAa,6BAA6B;IAIxC;;;OAGG;IACH,YAAY,MAAiC,EAAE,OAA+B;QAC5E,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IAED;;;OAGG;IACH,IAAI,UAAU;QACZ,IAAI,gBAAgB,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACrC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC;QACtC,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;OAEG;IACH,aAAa;QACX,yDAAyD;QACzD,MAAM,UAAU,GAAG,EAAE,CAAC;QACtB,IAAI,qBAAqB,GAAG,CAAC,CAAC;QAC9B,MAAM,UAAU,GAAG,IAAI,GAAG,EAAkB,CAAC;QAC7C,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;YAChC,MAAM,EAAE,GAAG,KAAK,CAAC,SAAS,CAAC;YAC3B,MAAM,KAAK,GAAG,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;YACjC,IAAI,KAAK,IAAI,IAAI,EAAE,CAAC;gBAClB,UAAU,CAAC,IAAI,CAAC,cAAc,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC;YAChD,CAAC;iBAAM,CAAC;gBACN,UAAU,CAAC,GAAG,CAAC,EAAE,EAAE,qBAAqB,CAAC,CAAC;gBAC1C,UAAU,CAAC,IAAI,CAAC,cAAc,CAAC,KAAK,EAAE,qBAAqB,CAAC,CAAC,CAAC;gBAC9D,qBAAqB,EAAE,CAAC;YAC1B,CAAC;QACH,CAAC;QAED,MAA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K,EAAE,KAAK,CAAC,CAAC;QAChD,KAAK,WAAW;YACd,OAAO,IAAA,+BAAuB,EAAC,KAAK,EAAE,KAAK,CAAC,CAAC;QAC/C,KAAK,YAAY;YACf,OAAO,IAAA,gCAAwB,EAAC,KAAK,EAAE,KAAK,CAAC,CAAC;QAChD,KAAK,YAAY;YACf,OAAO,IAAA,gCAAwB,EAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IAClD,CAAC;AACH,CAAC"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=common.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"common.js","sourceRoot":"","sources":["../../../src/operations/client_bulk_write/common.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mongodb",
3
- "version": "6.8.0-dev.20240824.sha.40ace73c",
3
+ "version": "6.8.0-dev.20240829.sha.6d65ae77",
4
4
  "description": "The official MongoDB driver for Node.js",
5
5
  "main": "lib/index.js",
6
6
  "files": [
@@ -0,0 +1,283 @@
1
+ import { type Document } from '../../bson';
2
+ import { DocumentSequence } from '../../cmap/commands';
3
+ import type { Filter, OptionalId, UpdateFilter, WithoutId } from '../../mongo_types';
4
+ import { type CollationOptions } from '../command';
5
+ import { type Hint } from '../operation';
6
+ import type {
7
+ AnyClientBulkWriteModel,
8
+ ClientBulkWriteOptions,
9
+ ClientDeleteManyModel,
10
+ ClientDeleteOneModel,
11
+ ClientInsertOneModel,
12
+ ClientReplaceOneModel,
13
+ ClientUpdateManyModel,
14
+ ClientUpdateOneModel
15
+ } from './common';
16
+
17
+ /** @internal */
18
+ export interface ClientBulkWriteCommand {
19
+ bulkWrite: 1;
20
+ errorsOnly: boolean;
21
+ ordered: boolean;
22
+ ops: DocumentSequence;
23
+ nsInfo: DocumentSequence;
24
+ bypassDocumentValidation?: boolean;
25
+ let?: Document;
26
+ }
27
+
28
+ /** @internal */
29
+ export class ClientBulkWriteCommandBuilder {
30
+ models: AnyClientBulkWriteModel[];
31
+ options: ClientBulkWriteOptions;
32
+
33
+ /**
34
+ * Create the command builder.
35
+ * @param models - The client write models.
36
+ */
37
+ constructor(models: AnyClientBulkWriteModel[], options: ClientBulkWriteOptions) {
38
+ this.models = models;
39
+ this.options = options;
40
+ }
41
+
42
+ /**
43
+ * Gets the errorsOnly value for the command, which is the inverse of the
44
+ * user provided verboseResults option. Defaults to true.
45
+ */
46
+ get errorsOnly(): boolean {
47
+ if ('verboseResults' in this.options) {
48
+ return !this.options.verboseResults;
49
+ }
50
+ return true;
51
+ }
52
+
53
+ /**
54
+ * Build the bulk write commands from the models.
55
+ */
56
+ buildCommands(): ClientBulkWriteCommand[] {
57
+ // Iterate the models to build the ops and nsInfo fields.
58
+ const operations = [];
59
+ let currentNamespaceIndex = 0;
60
+ const namespaces = new Map<string, number>();
61
+ for (const model of this.models) {
62
+ const ns = model.namespace;
63
+ const index = namespaces.get(ns);
64
+ if (index != null) {
65
+ operations.push(buildOperation(model, index));
66
+ } else {
67
+ namespaces.set(ns, currentNamespaceIndex);
68
+ operations.push(buildOperation(model, currentNamespaceIndex));
69
+ currentNamespaceIndex++;
70
+ }
71
+ }
72
+
73
+ const nsInfo = Array.from(namespaces.keys(), ns => ({ ns }));
74
+
75
+ // The base command.
76
+ const command: ClientBulkWriteCommand = {
77
+ bulkWrite: 1,
78
+ errorsOnly: this.errorsOnly,
79
+ ordered: this.options.ordered ?? true,
80
+ ops: new DocumentSequence(operations),
81
+ nsInfo: new DocumentSequence(nsInfo)
82
+ };
83
+ // Add bypassDocumentValidation if it was present in the options.
84
+ if (this.options.bypassDocumentValidation != null) {
85
+ command.bypassDocumentValidation = this.options.bypassDocumentValidation;
86
+ }
87
+ // Add let if it was present in the options.
88
+ if (this.options.let) {
89
+ command.let = this.options.let;
90
+ }
91
+ return [command];
92
+ }
93
+ }
94
+
95
+ /** @internal */
96
+ interface ClientInsertOperation {
97
+ insert: number;
98
+ document: OptionalId<Document>;
99
+ }
100
+
101
+ /**
102
+ * Build the insert one operation.
103
+ * @param model - The insert one model.
104
+ * @param index - The namespace index.
105
+ * @returns the operation.
106
+ */
107
+ export const buildInsertOneOperation = (
108
+ model: ClientInsertOneModel,
109
+ index: number
110
+ ): ClientInsertOperation => {
111
+ const document: ClientInsertOperation = {
112
+ insert: index,
113
+ document: model.document
114
+ };
115
+ return document;
116
+ };
117
+
118
+ /** @internal */
119
+ export interface ClientDeleteOperation {
120
+ delete: number;
121
+ multi: boolean;
122
+ filter: Filter<Document>;
123
+ hint?: Hint;
124
+ collation?: CollationOptions;
125
+ }
126
+
127
+ /**
128
+ * Build the delete one operation.
129
+ * @param model - The insert many model.
130
+ * @param index - The namespace index.
131
+ * @returns the operation.
132
+ */
133
+ export const buildDeleteOneOperation = (model: ClientDeleteOneModel, index: number): Document => {
134
+ return createDeleteOperation(model, index, false);
135
+ };
136
+
137
+ /**
138
+ * Build the delete many operation.
139
+ * @param model - The delete many model.
140
+ * @param index - The namespace index.
141
+ * @returns the operation.
142
+ */
143
+ export const buildDeleteManyOperation = (model: ClientDeleteManyModel, index: number): Document => {
144
+ return createDeleteOperation(model, index, true);
145
+ };
146
+
147
+ /**
148
+ * Creates a delete operation based on the parameters.
149
+ */
150
+ function createDeleteOperation(
151
+ model: ClientDeleteOneModel | ClientDeleteManyModel,
152
+ index: number,
153
+ multi: boolean
154
+ ): ClientDeleteOperation {
155
+ const document: ClientDeleteOperation = {
156
+ delete: index,
157
+ multi: multi,
158
+ filter: model.filter
159
+ };
160
+ if (model.hint) {
161
+ document.hint = model.hint;
162
+ }
163
+ if (model.collation) {
164
+ document.collation = model.collation;
165
+ }
166
+ return document;
167
+ }
168
+
169
+ /** @internal */
170
+ export interface ClientUpdateOperation {
171
+ update: number;
172
+ multi: boolean;
173
+ filter: Filter<Document>;
174
+ updateMods: UpdateFilter<Document> | Document[];
175
+ hint?: Hint;
176
+ upsert?: boolean;
177
+ arrayFilters?: Document[];
178
+ }
179
+
180
+ /**
181
+ * Build the update one operation.
182
+ * @param model - The update one model.
183
+ * @param index - The namespace index.
184
+ * @returns the operation.
185
+ */
186
+ export const buildUpdateOneOperation = (
187
+ model: ClientUpdateOneModel,
188
+ index: number
189
+ ): ClientUpdateOperation => {
190
+ return createUpdateOperation(model, index, false);
191
+ };
192
+
193
+ /**
194
+ * Build the update many operation.
195
+ * @param model - The update many model.
196
+ * @param index - The namespace index.
197
+ * @returns the operation.
198
+ */
199
+ export const buildUpdateManyOperation = (
200
+ model: ClientUpdateManyModel,
201
+ index: number
202
+ ): ClientUpdateOperation => {
203
+ return createUpdateOperation(model, index, true);
204
+ };
205
+
206
+ /**
207
+ * Creates a delete operation based on the parameters.
208
+ */
209
+ function createUpdateOperation(
210
+ model: ClientUpdateOneModel | ClientUpdateManyModel,
211
+ index: number,
212
+ multi: boolean
213
+ ): ClientUpdateOperation {
214
+ const document: ClientUpdateOperation = {
215
+ update: index,
216
+ multi: multi,
217
+ filter: model.filter,
218
+ updateMods: model.update
219
+ };
220
+ if (model.hint) {
221
+ document.hint = model.hint;
222
+ }
223
+ if (model.upsert) {
224
+ document.upsert = model.upsert;
225
+ }
226
+ if (model.arrayFilters) {
227
+ document.arrayFilters = model.arrayFilters;
228
+ }
229
+ return document;
230
+ }
231
+
232
+ /** @internal */
233
+ export interface ClientReplaceOneOperation {
234
+ update: number;
235
+ multi: boolean;
236
+ filter: Filter<Document>;
237
+ updateMods: WithoutId<Document>;
238
+ hint?: Hint;
239
+ upsert?: boolean;
240
+ }
241
+
242
+ /**
243
+ * Build the replace one operation.
244
+ * @param model - The replace one model.
245
+ * @param index - The namespace index.
246
+ * @returns the operation.
247
+ */
248
+ export const buildReplaceOneOperation = (
249
+ model: ClientReplaceOneModel,
250
+ index: number
251
+ ): ClientReplaceOneOperation => {
252
+ const document: ClientReplaceOneOperation = {
253
+ update: index,
254
+ multi: false,
255
+ filter: model.filter,
256
+ updateMods: model.replacement
257
+ };
258
+ if (model.hint) {
259
+ document.hint = model.hint;
260
+ }
261
+ if (model.upsert) {
262
+ document.upsert = model.upsert;
263
+ }
264
+ return document;
265
+ };
266
+
267
+ /** @internal */
268
+ export function buildOperation(model: AnyClientBulkWriteModel, index: number): Document {
269
+ switch (model.name) {
270
+ case 'insertOne':
271
+ return buildInsertOneOperation(model, index);
272
+ case 'deleteOne':
273
+ return buildDeleteOneOperation(model, index);
274
+ case 'deleteMany':
275
+ return buildDeleteManyOperation(model, index);
276
+ case 'updateOne':
277
+ return buildUpdateOneOperation(model, index);
278
+ case 'updateMany':
279
+ return buildUpdateManyOperation(model, index);
280
+ case 'replaceOne':
281
+ return buildReplaceOneOperation(model, index);
282
+ }
283
+ }
@@ -0,0 +1,146 @@
1
+ import { type Document } from '../../bson';
2
+ import type { Filter, OptionalId, UpdateFilter, WithoutId } from '../../mongo_types';
3
+ import type { CollationOptions, CommandOperationOptions } from '../../operations/command';
4
+ import type { Hint } from '../../operations/operation';
5
+
6
+ /** @public */
7
+ export interface ClientBulkWriteOptions extends CommandOperationOptions {
8
+ /**
9
+ * If true, when an insert fails, don't execute the remaining writes.
10
+ * If false, continue with remaining inserts when one fails.
11
+ * @defaultValue `true` - inserts are ordered by default
12
+ */
13
+ ordered?: boolean;
14
+ /**
15
+ * Allow driver to bypass schema validation.
16
+ * @defaultValue `false` - documents will be validated by default
17
+ **/
18
+ bypassDocumentValidation?: boolean;
19
+ /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */
20
+ let?: Document;
21
+ /**
22
+ * Whether detailed results for each successful operation should be included in the returned
23
+ * BulkWriteResult.
24
+ */
25
+ verboseResults?: boolean;
26
+ }
27
+
28
+ /** @public */
29
+ export interface ClientWriteModel {
30
+ /** The namespace for the write. */
31
+ namespace: string;
32
+ }
33
+
34
+ /** @public */
35
+ export interface ClientInsertOneModel extends ClientWriteModel {
36
+ name: 'insertOne';
37
+ /** The document to insert. */
38
+ document: OptionalId<Document>;
39
+ }
40
+
41
+ /** @public */
42
+ export interface ClientDeleteOneModel extends ClientWriteModel {
43
+ name: 'deleteOne';
44
+ /**
45
+ * The filter used to determine if a document should be deleted.
46
+ * For a deleteOne operation, the first match is removed.
47
+ */
48
+ filter: Filter<Document>;
49
+ /** Specifies a collation. */
50
+ collation?: CollationOptions;
51
+ /** The index to use. If specified, then the query system will only consider plans using the hinted index. */
52
+ hint?: Hint;
53
+ }
54
+
55
+ /** @public */
56
+ export interface ClientDeleteManyModel extends ClientWriteModel {
57
+ name: 'deleteMany';
58
+ /**
59
+ * The filter used to determine if a document should be deleted.
60
+ * For a deleteMany operation, all matches are removed.
61
+ */
62
+ filter: Filter<Document>;
63
+ /** Specifies a collation. */
64
+ collation?: CollationOptions;
65
+ /** The index to use. If specified, then the query system will only consider plans using the hinted index. */
66
+ hint?: Hint;
67
+ }
68
+
69
+ /** @public */
70
+ export interface ClientReplaceOneModel extends ClientWriteModel {
71
+ name: 'replaceOne';
72
+ /**
73
+ * The filter used to determine if a document should be replaced.
74
+ * For a replaceOne operation, the first match is replaced.
75
+ */
76
+ filter: Filter<Document>;
77
+ /** The document with which to replace the matched document. */
78
+ replacement: WithoutId<Document>;
79
+ /** Specifies a collation. */
80
+ collation?: CollationOptions;
81
+ /** The index to use. If specified, then the query system will only consider plans using the hinted index. */
82
+ hint?: Hint;
83
+ /** When true, creates a new document if no document matches the query. */
84
+ upsert?: boolean;
85
+ }
86
+
87
+ /** @public */
88
+ export interface ClientUpdateOneModel extends ClientWriteModel {
89
+ name: 'updateOne';
90
+ /**
91
+ * The filter used to determine if a document should be updated.
92
+ * For an updateOne operation, the first match is updated.
93
+ */
94
+ filter: Filter<Document>;
95
+ /**
96
+ * The modifications to apply. The value can be either:
97
+ * UpdateFilter<Document> - A document that contains update operator expressions,
98
+ * Document[] - an aggregation pipeline.
99
+ */
100
+ update: UpdateFilter<Document> | Document[];
101
+ /** A set of filters specifying to which array elements an update should apply. */
102
+ arrayFilters?: Document[];
103
+ /** Specifies a collation. */
104
+ collation?: CollationOptions;
105
+ /** The index to use. If specified, then the query system will only consider plans using the hinted index. */
106
+ hint?: Hint;
107
+ /** When true, creates a new document if no document matches the query. */
108
+ upsert?: boolean;
109
+ }
110
+
111
+ /** @public */
112
+ export interface ClientUpdateManyModel extends ClientWriteModel {
113
+ name: 'updateMany';
114
+ /**
115
+ * The filter used to determine if a document should be updated.
116
+ * For an updateMany operation, all matches are updated.
117
+ */
118
+ filter: Filter<Document>;
119
+ /**
120
+ * The modifications to apply. The value can be either:
121
+ * UpdateFilter<Document> - A document that contains update operator expressions,
122
+ * Document[] - an aggregation pipeline.
123
+ */
124
+ update: UpdateFilter<Document> | Document[];
125
+ /** A set of filters specifying to which array elements an update should apply. */
126
+ arrayFilters?: Document[];
127
+ /** Specifies a collation. */
128
+ collation?: CollationOptions;
129
+ /** The index to use. If specified, then the query system will only consider plans using the hinted index. */
130
+ hint?: Hint;
131
+ /** When true, creates a new document if no document matches the query. */
132
+ upsert?: boolean;
133
+ }
134
+
135
+ /**
136
+ * Used to represent any of the client bulk write models that can be passed as an array
137
+ * to MongoClient#bulkWrite.
138
+ * @public
139
+ */
140
+ export type AnyClientBulkWriteModel =
141
+ | ClientInsertOneModel
142
+ | ClientReplaceOneModel
143
+ | ClientUpdateOneModel
144
+ | ClientUpdateManyModel
145
+ | ClientDeleteOneModel
146
+ | ClientDeleteManyModel;