* 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
)
| 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 |