MCPcopy
hub / github.com/mongodb/node-mongodb-native / bulkWrite

Method bulkWrite

src/collection.ts:362–392  ·  view source on GitHub ↗

* Perform a bulkWrite operation without a fluent API * * Legal operation types are * - `insertOne` * - `replaceOne` * - `updateOne` * - `updateMany` * - `deleteOne` * - `deleteMany` * * If documents passed in do not contain the **_id** field, * one will be added to e

(
    operations: ReadonlyArray<AnyBulkWriteOperation<TSchema>>,
    options?: BulkWriteOptions
  )

Source from the content-addressed store, hash-verified

360 * @throws MongoDriverError if operations is not an array
361 */
362 async bulkWrite(
363 operations: ReadonlyArray<AnyBulkWriteOperation<TSchema>>,
364 options?: BulkWriteOptions
365 ): Promise<BulkWriteResult> {
366 if (!Array.isArray(operations)) {
367 throw new MongoInvalidArgumentError('Argument "operations" must be an array of documents');
368 }
369
370 options = resolveOptions(this, options ?? {});
371
372 // TODO(NODE-7071): remove once the client doesn't need to be connected to construct
373 // bulk operations
374 const isConnected = this.client.topology != null;
375 if (!isConnected) {
376 await autoConnect(this.client);
377 }
378
379 // Create the bulk operation
380 const bulk: BulkOperationBase =
381 options.ordered === false
382 ? this.initializeUnorderedBulkOp(options)
383 : this.initializeOrderedBulkOp(options);
384
385 // for each op go through and add to the bulk
386 for (const operation of operations) {
387 bulk.raw(operation);
388 }
389
390 // Execute the bulk
391 return await bulk.execute({ ...options });
392 }
393
394 /**
395 * Update a single document in a collection

Callers 15

insertManyMethod · 0.95
operations.tsFile · 0.45
crud_api.test.tsFile · 0.45
bulk.test.tsFile · 0.45
testFunction · 0.45
crud.prose.test.tsFile · 0.45
driver.test.tsFile · 0.45

Calls 5

resolveOptionsFunction · 0.90
autoConnectFunction · 0.90
executeMethod · 0.65

Tested by 3

testFunction · 0.36
testBulkWriteFunction · 0.36