Source: lib/bulk/common.js

  1. 'use strict';
  2. const Long = require('../core').BSON.Long;
  3. const MongoError = require('../core').MongoError;
  4. const ObjectID = require('../core').BSON.ObjectID;
  5. const BSON = require('../core').BSON;
  6. const MongoWriteConcernError = require('../core').MongoWriteConcernError;
  7. const toError = require('../utils').toError;
  8. const handleCallback = require('../utils').handleCallback;
  9. const applyRetryableWrites = require('../utils').applyRetryableWrites;
  10. const applyWriteConcern = require('../utils').applyWriteConcern;
  11. const executeLegacyOperation = require('../utils').executeLegacyOperation;
  12. const isPromiseLike = require('../utils').isPromiseLike;
  13. // Error codes
  14. const WRITE_CONCERN_ERROR = 64;
  15. // Insert types
  16. const INSERT = 1;
  17. const UPDATE = 2;
  18. const REMOVE = 3;
  19. const bson = new BSON([
  20. BSON.Binary,
  21. BSON.Code,
  22. BSON.DBRef,
  23. BSON.Decimal128,
  24. BSON.Double,
  25. BSON.Int32,
  26. BSON.Long,
  27. BSON.Map,
  28. BSON.MaxKey,
  29. BSON.MinKey,
  30. BSON.ObjectId,
  31. BSON.BSONRegExp,
  32. BSON.Symbol,
  33. BSON.Timestamp
  34. ]);
  35. /**
  36. * Keeps the state of a unordered batch so we can rewrite the results
  37. * correctly after command execution
  38. * @ignore
  39. */
  40. class Batch {
  41. constructor(batchType, originalZeroIndex) {
  42. this.originalZeroIndex = originalZeroIndex;
  43. this.currentIndex = 0;
  44. this.originalIndexes = [];
  45. this.batchType = batchType;
  46. this.operations = [];
  47. this.size = 0;
  48. this.sizeBytes = 0;
  49. }
  50. }
  51. /**
  52. * @classdesc
  53. * The result of a bulk write.
  54. */
  55. class BulkWriteResult {
  56. /**
  57. * Create a new BulkWriteResult instance
  58. *
  59. * **NOTE:** Internal Type, do not instantiate directly
  60. */
  61. constructor(bulkResult) {
  62. this.result = bulkResult;
  63. }
  64. /**
  65. * Evaluates to true if the bulk operation correctly executes
  66. * @type {boolean}
  67. */
  68. get ok() {
  69. return this.result.ok;
  70. }
  71. /**
  72. * The number of inserted documents
  73. * @type {number}
  74. */
  75. get nInserted() {
  76. return this.result.nInserted;
  77. }
  78. /**
  79. * Number of upserted documents
  80. * @type {number}
  81. */
  82. get nUpserted() {
  83. return this.result.nUpserted;
  84. }
  85. /**
  86. * Number of matched documents
  87. * @type {number}
  88. */
  89. get nMatched() {
  90. return this.result.nMatched;
  91. }
  92. /**
  93. * Number of documents updated physically on disk
  94. * @type {number}
  95. */
  96. get nModified() {
  97. return this.result.nModified;
  98. }
  99. /**
  100. * Number of removed documents
  101. * @type {number}
  102. */
  103. get nRemoved() {
  104. return this.result.nRemoved;
  105. }
  106. /**
  107. * Returns an array of all inserted ids
  108. *
  109. * @return {object[]}
  110. */
  111. getInsertedIds() {
  112. return this.result.insertedIds;
  113. }
  114. /**
  115. * Returns an array of all upserted ids
  116. *
  117. * @return {object[]}
  118. */
  119. getUpsertedIds() {
  120. return this.result.upserted;
  121. }
  122. /**
  123. * Returns the upserted id at the given index
  124. *
  125. * @param {number} index the number of the upserted id to return, returns undefined if no result for passed in index
  126. * @return {object}
  127. */
  128. getUpsertedIdAt(index) {
  129. return this.result.upserted[index];
  130. }
  131. /**
  132. * Returns raw internal result
  133. *
  134. * @return {object}
  135. */
  136. getRawResponse() {
  137. return this.result;
  138. }
  139. /**
  140. * Returns true if the bulk operation contains a write error
  141. *
  142. * @return {boolean}
  143. */
  144. hasWriteErrors() {
  145. return this.result.writeErrors.length > 0;
  146. }
  147. /**
  148. * Returns the number of write errors off the bulk operation
  149. *
  150. * @return {number}
  151. */
  152. getWriteErrorCount() {
  153. return this.result.writeErrors.length;
  154. }
  155. /**
  156. * Returns a specific write error object
  157. *
  158. * @param {number} index of the write error to return, returns null if there is no result for passed in index
  159. * @return {WriteError}
  160. */
  161. getWriteErrorAt(index) {
  162. if (index < this.result.writeErrors.length) {
  163. return this.result.writeErrors[index];
  164. }
  165. return null;
  166. }
  167. /**
  168. * Retrieve all write errors
  169. *
  170. * @return {WriteError[]}
  171. */
  172. getWriteErrors() {
  173. return this.result.writeErrors;
  174. }
  175. /**
  176. * Retrieve lastOp if available
  177. *
  178. * @return {object}
  179. */
  180. getLastOp() {
  181. return this.result.lastOp;
  182. }
  183. /**
  184. * Retrieve the write concern error if any
  185. *
  186. * @return {WriteConcernError}
  187. */
  188. getWriteConcernError() {
  189. if (this.result.writeConcernErrors.length === 0) {
  190. return null;
  191. } else if (this.result.writeConcernErrors.length === 1) {
  192. // Return the error
  193. return this.result.writeConcernErrors[0];
  194. } else {
  195. // Combine the errors
  196. let errmsg = '';
  197. for (let i = 0; i < this.result.writeConcernErrors.length; i++) {
  198. const err = this.result.writeConcernErrors[i];
  199. errmsg = errmsg + err.errmsg;
  200. // TODO: Something better
  201. if (i === 0) errmsg = errmsg + ' and ';
  202. }
  203. return new WriteConcernError({ errmsg: errmsg, code: WRITE_CONCERN_ERROR });
  204. }
  205. }
  206. /**
  207. * @return {object}
  208. */
  209. toJSON() {
  210. return this.result;
  211. }
  212. /**
  213. * @return {string}
  214. */
  215. toString() {
  216. return `BulkWriteResult(${this.toJSON(this.result)})`;
  217. }
  218. /**
  219. * @return {boolean}
  220. */
  221. isOk() {
  222. return this.result.ok === 1;
  223. }
  224. }
  225. /**
  226. * @classdesc An error representing a failure by the server to apply the requested write concern to the bulk operation.
  227. */
  228. class WriteConcernError {
  229. /**
  230. * Create a new WriteConcernError instance
  231. *
  232. * **NOTE:** Internal Type, do not instantiate directly
  233. */
  234. constructor(err) {
  235. this.err = err;
  236. }
  237. /**
  238. * Write concern error code.
  239. * @type {number}
  240. */
  241. get code() {
  242. return this.err.code;
  243. }
  244. /**
  245. * Write concern error message.
  246. * @type {string}
  247. */
  248. get errmsg() {
  249. return this.err.errmsg;
  250. }
  251. /**
  252. * @return {object}
  253. */
  254. toJSON() {
  255. return { code: this.err.code, errmsg: this.err.errmsg };
  256. }
  257. /**
  258. * @return {string}
  259. */
  260. toString() {
  261. return `WriteConcernError(${this.err.errmsg})`;
  262. }
  263. }
  264. /**
  265. * @classdesc An error that occurred during a BulkWrite on the server.
  266. */
  267. class WriteError {
  268. /**
  269. * Create a new WriteError instance
  270. *
  271. * **NOTE:** Internal Type, do not instantiate directly
  272. */
  273. constructor(err) {
  274. this.err = err;
  275. }
  276. /**
  277. * WriteError code.
  278. * @type {number}
  279. */
  280. get code() {
  281. return this.err.code;
  282. }
  283. /**
  284. * WriteError original bulk operation index.
  285. * @type {number}
  286. */
  287. get index() {
  288. return this.err.index;
  289. }
  290. /**
  291. * WriteError message.
  292. * @type {string}
  293. */
  294. get errmsg() {
  295. return this.err.errmsg;
  296. }
  297. /**
  298. * Returns the underlying operation that caused the error
  299. * @return {object}
  300. */
  301. getOperation() {
  302. return this.err.op;
  303. }
  304. /**
  305. * @return {object}
  306. */
  307. toJSON() {
  308. return { code: this.err.code, index: this.err.index, errmsg: this.err.errmsg, op: this.err.op };
  309. }
  310. /**
  311. * @return {string}
  312. */
  313. toString() {
  314. return `WriteError(${JSON.stringify(this.toJSON())})`;
  315. }
  316. }
  317. /**
  318. * Merges results into shared data structure
  319. * @ignore
  320. */
  321. function mergeBatchResults(batch, bulkResult, err, result) {
  322. // If we have an error set the result to be the err object
  323. if (err) {
  324. result = err;
  325. } else if (result && result.result) {
  326. result = result.result;
  327. } else if (result == null) {
  328. return;
  329. }
  330. // Do we have a top level error stop processing and return
  331. if (result.ok === 0 && bulkResult.ok === 1) {
  332. bulkResult.ok = 0;
  333. const writeError = {
  334. index: 0,
  335. code: result.code || 0,
  336. errmsg: result.message,
  337. op: batch.operations[0]
  338. };
  339. bulkResult.writeErrors.push(new WriteError(writeError));
  340. return;
  341. } else if (result.ok === 0 && bulkResult.ok === 0) {
  342. return;
  343. }
  344. // Deal with opTime if available
  345. if (result.opTime || result.lastOp) {
  346. const opTime = result.lastOp || result.opTime;
  347. let lastOpTS = null;
  348. let lastOpT = null;
  349. // We have a time stamp
  350. if (opTime && opTime._bsontype === 'Timestamp') {
  351. if (bulkResult.lastOp == null) {
  352. bulkResult.lastOp = opTime;
  353. } else if (opTime.greaterThan(bulkResult.lastOp)) {
  354. bulkResult.lastOp = opTime;
  355. }
  356. } else {
  357. // Existing TS
  358. if (bulkResult.lastOp) {
  359. lastOpTS =
  360. typeof bulkResult.lastOp.ts === 'number'
  361. ? Long.fromNumber(bulkResult.lastOp.ts)
  362. : bulkResult.lastOp.ts;
  363. lastOpT =
  364. typeof bulkResult.lastOp.t === 'number'
  365. ? Long.fromNumber(bulkResult.lastOp.t)
  366. : bulkResult.lastOp.t;
  367. }
  368. // Current OpTime TS
  369. const opTimeTS = typeof opTime.ts === 'number' ? Long.fromNumber(opTime.ts) : opTime.ts;
  370. const opTimeT = typeof opTime.t === 'number' ? Long.fromNumber(opTime.t) : opTime.t;
  371. // Compare the opTime's
  372. if (bulkResult.lastOp == null) {
  373. bulkResult.lastOp = opTime;
  374. } else if (opTimeTS.greaterThan(lastOpTS)) {
  375. bulkResult.lastOp = opTime;
  376. } else if (opTimeTS.equals(lastOpTS)) {
  377. if (opTimeT.greaterThan(lastOpT)) {
  378. bulkResult.lastOp = opTime;
  379. }
  380. }
  381. }
  382. }
  383. // If we have an insert Batch type
  384. if (batch.batchType === INSERT && result.n) {
  385. bulkResult.nInserted = bulkResult.nInserted + result.n;
  386. }
  387. // If we have an insert Batch type
  388. if (batch.batchType === REMOVE && result.n) {
  389. bulkResult.nRemoved = bulkResult.nRemoved + result.n;
  390. }
  391. let nUpserted = 0;
  392. // We have an array of upserted values, we need to rewrite the indexes
  393. if (Array.isArray(result.upserted)) {
  394. nUpserted = result.upserted.length;
  395. for (let i = 0; i < result.upserted.length; i++) {
  396. bulkResult.upserted.push({
  397. index: result.upserted[i].index + batch.originalZeroIndex,
  398. _id: result.upserted[i]._id
  399. });
  400. }
  401. } else if (result.upserted) {
  402. nUpserted = 1;
  403. bulkResult.upserted.push({
  404. index: batch.originalZeroIndex,
  405. _id: result.upserted
  406. });
  407. }
  408. // If we have an update Batch type
  409. if (batch.batchType === UPDATE && result.n) {
  410. const nModified = result.nModified;
  411. bulkResult.nUpserted = bulkResult.nUpserted + nUpserted;
  412. bulkResult.nMatched = bulkResult.nMatched + (result.n - nUpserted);
  413. if (typeof nModified === 'number') {
  414. bulkResult.nModified = bulkResult.nModified + nModified;
  415. } else {
  416. bulkResult.nModified = null;
  417. }
  418. }
  419. if (Array.isArray(result.writeErrors)) {
  420. for (let i = 0; i < result.writeErrors.length; i++) {
  421. const writeError = {
  422. index: batch.originalIndexes[i],
  423. code: result.writeErrors[i].code,
  424. errmsg: result.writeErrors[i].errmsg,
  425. op: batch.operations[result.writeErrors[i].index]
  426. };
  427. bulkResult.writeErrors.push(new WriteError(writeError));
  428. }
  429. }
  430. if (result.writeConcernError) {
  431. bulkResult.writeConcernErrors.push(new WriteConcernError(result.writeConcernError));
  432. }
  433. }
  434. function executeCommands(bulkOperation, options, callback) {
  435. if (bulkOperation.s.batches.length === 0) {
  436. return handleCallback(callback, null, new BulkWriteResult(bulkOperation.s.bulkResult));
  437. }
  438. const batch = bulkOperation.s.batches.shift();
  439. function resultHandler(err, result) {
  440. // Error is a driver related error not a bulk op error, terminate
  441. if (((err && err.driver) || (err && err.message)) && !(err instanceof MongoWriteConcernError)) {
  442. return handleCallback(callback, err);
  443. }
  444. // If we have and error
  445. if (err) err.ok = 0;
  446. if (err instanceof MongoWriteConcernError) {
  447. return handleMongoWriteConcernError(batch, bulkOperation.s.bulkResult, err, callback);
  448. }
  449. // Merge the results together
  450. const writeResult = new BulkWriteResult(bulkOperation.s.bulkResult);
  451. const mergeResult = mergeBatchResults(batch, bulkOperation.s.bulkResult, err, result);
  452. if (mergeResult != null) {
  453. return handleCallback(callback, null, writeResult);
  454. }
  455. if (bulkOperation.handleWriteError(callback, writeResult)) return;
  456. // Execute the next command in line
  457. executeCommands(bulkOperation, options, callback);
  458. }
  459. bulkOperation.finalOptionsHandler({ options, batch, resultHandler }, callback);
  460. }
  461. /**
  462. * handles write concern error
  463. *
  464. * @ignore
  465. * @param {object} batch
  466. * @param {object} bulkResult
  467. * @param {boolean} ordered
  468. * @param {WriteConcernError} err
  469. * @param {function} callback
  470. */
  471. function handleMongoWriteConcernError(batch, bulkResult, err, callback) {
  472. mergeBatchResults(batch, bulkResult, null, err.result);
  473. const wrappedWriteConcernError = new WriteConcernError({
  474. errmsg: err.result.writeConcernError.errmsg,
  475. code: err.result.writeConcernError.result
  476. });
  477. return handleCallback(
  478. callback,
  479. new BulkWriteError(toError(wrappedWriteConcernError), new BulkWriteResult(bulkResult)),
  480. null
  481. );
  482. }
  483. /**
  484. * @classdesc An error indicating an unsuccessful Bulk Write
  485. */
  486. class BulkWriteError extends MongoError {
  487. /**
  488. * Creates a new BulkWriteError
  489. *
  490. * @param {Error|string|object} message The error message
  491. * @param {BulkWriteResult} result The result of the bulk write operation
  492. * @extends {MongoError}
  493. */
  494. constructor(error, result) {
  495. const message = error.err || error.errmsg || error.errMessage || error;
  496. super(message);
  497. Object.assign(this, error);
  498. this.name = 'BulkWriteError';
  499. this.result = result;
  500. }
  501. }
  502. /**
  503. * @classdesc A builder object that is returned from {@link BulkOperationBase#find}.
  504. * Is used to build a write operation that involves a query filter.
  505. */
  506. class FindOperators {
  507. /**
  508. * Creates a new FindOperators object.
  509. *
  510. * **NOTE:** Internal Type, do not instantiate directly
  511. * @param {OrderedBulkOperation|UnorderedBulkOperation} bulkOperation
  512. */
  513. constructor(bulkOperation) {
  514. this.s = bulkOperation.s;
  515. }
  516. /**
  517. * Add a multiple update operation to the bulk operation
  518. *
  519. * @method
  520. * @param {object} updateDocument An update field for an update operation. See {@link https://www.mongodb.com/docs/manual/reference/command/update/#update-command-u u documentation}
  521. * @param {object} [options.hint] An optional hint for query optimization. See the {@link https://www.mongodb.com/docs/manual/reference/command/update/#update-command-hint|update command} reference for more information.
  522. * @throws {MongoError} If operation cannot be added to bulk write
  523. * @return {OrderedBulkOperation|UnorderedBulkOperation} A reference to the parent BulkOperation
  524. */
  525. update(updateDocument) {
  526. // Perform upsert
  527. const upsert = typeof this.s.currentOp.upsert === 'boolean' ? this.s.currentOp.upsert : false;
  528. // Establish the update command
  529. const document = {
  530. q: this.s.currentOp.selector,
  531. u: updateDocument,
  532. multi: true,
  533. upsert: upsert
  534. };
  535. if (updateDocument.hint) {
  536. document.hint = updateDocument.hint;
  537. }
  538. // Clear out current Op
  539. this.s.currentOp = null;
  540. return this.s.options.addToOperationsList(this, UPDATE, document);
  541. }
  542. /**
  543. * Add a single update operation to the bulk operation
  544. *
  545. * @method
  546. * @param {object} updateDocument An update field for an update operation. See {@link https://www.mongodb.com/docs/manual/reference/command/update/#update-command-u u documentation}
  547. * @param {object} [options.hint] An optional hint for query optimization. See the {@link https://www.mongodb.com/docs/manual/reference/command/update/#update-command-hint|update command} reference for more information.
  548. * @throws {MongoError} If operation cannot be added to bulk write
  549. * @return {OrderedBulkOperation|UnorderedBulkOperation} A reference to the parent BulkOperation
  550. */
  551. updateOne(updateDocument) {
  552. // Perform upsert
  553. const upsert = typeof this.s.currentOp.upsert === 'boolean' ? this.s.currentOp.upsert : false;
  554. // Establish the update command
  555. const document = {
  556. q: this.s.currentOp.selector,
  557. u: updateDocument,
  558. multi: false,
  559. upsert: upsert
  560. };
  561. if (updateDocument.hint) {
  562. document.hint = updateDocument.hint;
  563. }
  564. // Clear out current Op
  565. this.s.currentOp = null;
  566. return this.s.options.addToOperationsList(this, UPDATE, document);
  567. }
  568. /**
  569. * Add a replace one operation to the bulk operation
  570. *
  571. * @method
  572. * @param {object} updateDocument the new document to replace the existing one with
  573. * @throws {MongoError} If operation cannot be added to bulk write
  574. * @return {OrderedBulkOperation|UnorderedBulkOperation} A reference to the parent BulkOperation
  575. */
  576. replaceOne(updateDocument) {
  577. this.updateOne(updateDocument);
  578. }
  579. /**
  580. * Upsert modifier for update bulk operation, noting that this operation is an upsert.
  581. *
  582. * @method
  583. * @throws {MongoError} If operation cannot be added to bulk write
  584. * @return {FindOperators} reference to self
  585. */
  586. upsert() {
  587. this.s.currentOp.upsert = true;
  588. return this;
  589. }
  590. /**
  591. * Add a delete one operation to the bulk operation
  592. *
  593. * @method
  594. * @throws {MongoError} If operation cannot be added to bulk write
  595. * @return {OrderedBulkOperation|UnorderedBulkOperation} A reference to the parent BulkOperation
  596. */
  597. deleteOne() {
  598. // Establish the update command
  599. const document = {
  600. q: this.s.currentOp.selector,
  601. limit: 1
  602. };
  603. // Clear out current Op
  604. this.s.currentOp = null;
  605. return this.s.options.addToOperationsList(this, REMOVE, document);
  606. }
  607. /**
  608. * Add a delete many operation to the bulk operation
  609. *
  610. * @method
  611. * @throws {MongoError} If operation cannot be added to bulk write
  612. * @return {OrderedBulkOperation|UnorderedBulkOperation} A reference to the parent BulkOperation
  613. */
  614. delete() {
  615. // Establish the update command
  616. const document = {
  617. q: this.s.currentOp.selector,
  618. limit: 0
  619. };
  620. // Clear out current Op
  621. this.s.currentOp = null;
  622. return this.s.options.addToOperationsList(this, REMOVE, document);
  623. }
  624. /**
  625. * backwards compatability for deleteOne
  626. */
  627. removeOne() {
  628. return this.deleteOne();
  629. }
  630. /**
  631. * backwards compatability for delete
  632. */
  633. remove() {
  634. return this.delete();
  635. }
  636. }
  637. /**
  638. * @classdesc Parent class to OrderedBulkOperation and UnorderedBulkOperation
  639. *
  640. * **NOTE:** Internal Type, do not instantiate directly
  641. */
  642. class BulkOperationBase {
  643. /**
  644. * Create a new OrderedBulkOperation or UnorderedBulkOperation instance
  645. * @property {number} length Get the number of operations in the bulk.
  646. */
  647. constructor(topology, collection, options, isOrdered) {
  648. // determine whether bulkOperation is ordered or unordered
  649. this.isOrdered = isOrdered;
  650. options = options == null ? {} : options;
  651. // TODO Bring from driver information in isMaster
  652. // Get the namespace for the write operations
  653. const namespace = collection.s.namespace;
  654. // Used to mark operation as executed
  655. const executed = false;
  656. // Current item
  657. const currentOp = null;
  658. // Handle to the bson serializer, used to calculate running sizes
  659. const bson = topology.bson;
  660. // Set max byte size
  661. const isMaster = topology.lastIsMaster();
  662. const maxBatchSizeBytes =
  663. isMaster && isMaster.maxBsonObjectSize ? isMaster.maxBsonObjectSize : 1024 * 1024 * 16;
  664. const maxWriteBatchSize =
  665. isMaster && isMaster.maxWriteBatchSize ? isMaster.maxWriteBatchSize : 1000;
  666. // Calculates the largest possible size of an Array key, represented as a BSON string
  667. // element. This calculation:
  668. // 1 byte for BSON type
  669. // # of bytes = length of (string representation of (maxWriteBatchSize - 1))
  670. // + 1 bytes for null terminator
  671. const maxKeySize = (maxWriteBatchSize - 1).toString(10).length + 2;
  672. // Final options for retryable writes and write concern
  673. let finalOptions = Object.assign({}, options);
  674. finalOptions = applyRetryableWrites(finalOptions, collection.s.db);
  675. finalOptions = applyWriteConcern(finalOptions, { collection: collection }, options);
  676. const writeConcern = finalOptions.writeConcern;
  677. // Get the promiseLibrary
  678. const promiseLibrary = options.promiseLibrary || Promise;
  679. // Final results
  680. const bulkResult = {
  681. ok: 1,
  682. writeErrors: [],
  683. writeConcernErrors: [],
  684. insertedIds: [],
  685. nInserted: 0,
  686. nUpserted: 0,
  687. nMatched: 0,
  688. nModified: 0,
  689. nRemoved: 0,
  690. upserted: []
  691. };
  692. // Internal state
  693. this.s = {
  694. // Final result
  695. bulkResult: bulkResult,
  696. // Current batch state
  697. currentBatch: null,
  698. currentIndex: 0,
  699. // ordered specific
  700. currentBatchSize: 0,
  701. currentBatchSizeBytes: 0,
  702. // unordered specific
  703. currentInsertBatch: null,
  704. currentUpdateBatch: null,
  705. currentRemoveBatch: null,
  706. batches: [],
  707. // Write concern
  708. writeConcern: writeConcern,
  709. // Max batch size options
  710. maxBatchSizeBytes: maxBatchSizeBytes,
  711. maxWriteBatchSize: maxWriteBatchSize,
  712. maxKeySize,
  713. // Namespace
  714. namespace: namespace,
  715. // BSON
  716. bson: bson,
  717. // Topology
  718. topology: topology,
  719. // Options
  720. options: finalOptions,
  721. // Current operation
  722. currentOp: currentOp,
  723. // Executed
  724. executed: executed,
  725. // Collection
  726. collection: collection,
  727. // Promise Library
  728. promiseLibrary: promiseLibrary,
  729. // Fundamental error
  730. err: null,
  731. // check keys
  732. checkKeys: typeof options.checkKeys === 'boolean' ? options.checkKeys : true
  733. };
  734. // bypass Validation
  735. if (options.bypassDocumentValidation === true) {
  736. this.s.bypassDocumentValidation = true;
  737. }
  738. }
  739. /**
  740. * Add a single insert document to the bulk operation
  741. *
  742. * @param {object} document the document to insert
  743. * @throws {MongoError}
  744. * @return {BulkOperationBase} A reference to self
  745. *
  746. * @example
  747. * const bulkOp = collection.initializeOrderedBulkOp();
  748. * // Adds three inserts to the bulkOp.
  749. * bulkOp
  750. * .insert({ a: 1 })
  751. * .insert({ b: 2 })
  752. * .insert({ c: 3 });
  753. * await bulkOp.execute();
  754. */
  755. insert(document) {
  756. if (this.s.collection.s.db.options.forceServerObjectId !== true && document._id == null)
  757. document._id = new ObjectID();
  758. return this.s.options.addToOperationsList(this, INSERT, document);
  759. }
  760. /**
  761. * Builds a find operation for an update/updateOne/delete/deleteOne/replaceOne.
  762. * Returns a builder object used to complete the definition of the operation.
  763. *
  764. * @method
  765. * @param {object} selector The selector for the bulk operation. See {@link https://www.mongodb.com/docs/manual/reference/command/update/#update-command-q q documentation}
  766. * @throws {MongoError} if a selector is not specified
  767. * @return {FindOperators} A helper object with which the write operation can be defined.
  768. *
  769. * @example
  770. * const bulkOp = collection.initializeOrderedBulkOp();
  771. *
  772. * // Add an updateOne to the bulkOp
  773. * bulkOp.find({ a: 1 }).updateOne({ $set: { b: 2 } });
  774. *
  775. * // Add an updateMany to the bulkOp
  776. * bulkOp.find({ c: 3 }).update({ $set: { d: 4 } });
  777. *
  778. * // Add an upsert
  779. * bulkOp.find({ e: 5 }).upsert().updateOne({ $set: { f: 6 } });
  780. *
  781. * // Add a deletion
  782. * bulkOp.find({ g: 7 }).deleteOne();
  783. *
  784. * // Add a multi deletion
  785. * bulkOp.find({ h: 8 }).delete();
  786. *
  787. * // Add a replaceOne
  788. * bulkOp.find({ i: 9 }).replaceOne({ j: 10 });
  789. *
  790. * // Update using a pipeline (requires Mongodb 4.2 or higher)
  791. * bulk.find({ k: 11, y: { $exists: true }, z: { $exists: true } }).updateOne([
  792. * { $set: { total: { $sum: [ '$y', '$z' ] } } }
  793. * ]);
  794. *
  795. * // All of the ops will now be executed
  796. * await bulkOp.execute();
  797. */
  798. find(selector) {
  799. if (!selector) {
  800. throw toError('Bulk find operation must specify a selector');
  801. }
  802. // Save a current selector
  803. this.s.currentOp = {
  804. selector: selector
  805. };
  806. return new FindOperators(this);
  807. }
  808. /**
  809. * Specifies a raw operation to perform in the bulk write.
  810. *
  811. * @method
  812. * @param {object} op The raw operation to perform.
  813. * @param {object} [options.hint] An optional hint for query optimization. See the {@link https://www.mongodb.com/docs/manual/reference/command/update/#update-command-hint|update command} reference for more information.
  814. * @return {BulkOperationBase} A reference to self
  815. */
  816. raw(op) {
  817. const key = Object.keys(op)[0];
  818. // Set up the force server object id
  819. const forceServerObjectId =
  820. typeof this.s.options.forceServerObjectId === 'boolean'
  821. ? this.s.options.forceServerObjectId
  822. : this.s.collection.s.db.options.forceServerObjectId;
  823. // Update operations
  824. if (
  825. (op.updateOne && op.updateOne.q) ||
  826. (op.updateMany && op.updateMany.q) ||
  827. (op.replaceOne && op.replaceOne.q)
  828. ) {
  829. op[key].multi = op.updateOne || op.replaceOne ? false : true;
  830. return this.s.options.addToOperationsList(this, UPDATE, op[key]);
  831. }
  832. // Crud spec update format
  833. if (op.updateOne || op.updateMany || op.replaceOne) {
  834. const multi = op.updateOne || op.replaceOne ? false : true;
  835. const operation = {
  836. q: op[key].filter,
  837. u: op[key].update || op[key].replacement,
  838. multi: multi
  839. };
  840. if (op[key].hint) {
  841. operation.hint = op[key].hint;
  842. }
  843. if (this.isOrdered) {
  844. operation.upsert = op[key].upsert ? true : false;
  845. if (op.collation) operation.collation = op.collation;
  846. } else {
  847. if (op[key].upsert) operation.upsert = true;
  848. }
  849. if (op[key].arrayFilters) operation.arrayFilters = op[key].arrayFilters;
  850. return this.s.options.addToOperationsList(this, UPDATE, operation);
  851. }
  852. // Remove operations
  853. if (
  854. op.removeOne ||
  855. op.removeMany ||
  856. (op.deleteOne && op.deleteOne.q) ||
  857. (op.deleteMany && op.deleteMany.q)
  858. ) {
  859. op[key].limit = op.removeOne ? 1 : 0;
  860. return this.s.options.addToOperationsList(this, REMOVE, op[key]);
  861. }
  862. // Crud spec delete operations, less efficient
  863. if (op.deleteOne || op.deleteMany) {
  864. const limit = op.deleteOne ? 1 : 0;
  865. const operation = { q: op[key].filter, limit: limit };
  866. if (this.isOrdered) {
  867. if (op.collation) operation.collation = op.collation;
  868. }
  869. return this.s.options.addToOperationsList(this, REMOVE, operation);
  870. }
  871. // Insert operations
  872. if (op.insertOne && op.insertOne.document == null) {
  873. if (forceServerObjectId !== true && op.insertOne._id == null)
  874. op.insertOne._id = new ObjectID();
  875. return this.s.options.addToOperationsList(this, INSERT, op.insertOne);
  876. } else if (op.insertOne && op.insertOne.document) {
  877. if (forceServerObjectId !== true && op.insertOne.document._id == null)
  878. op.insertOne.document._id = new ObjectID();
  879. return this.s.options.addToOperationsList(this, INSERT, op.insertOne.document);
  880. }
  881. if (op.insertMany) {
  882. for (let i = 0; i < op.insertMany.length; i++) {
  883. if (forceServerObjectId !== true && op.insertMany[i]._id == null)
  884. op.insertMany[i]._id = new ObjectID();
  885. this.s.options.addToOperationsList(this, INSERT, op.insertMany[i]);
  886. }
  887. return;
  888. }
  889. // No valid type of operation
  890. throw toError(
  891. 'bulkWrite only supports insertOne, insertMany, updateOne, updateMany, removeOne, removeMany, deleteOne, deleteMany'
  892. );
  893. }
  894. /**
  895. * helper function to assist with promiseOrCallback behavior
  896. * @ignore
  897. * @param {*} err
  898. * @param {*} callback
  899. */
  900. _handleEarlyError(err, callback) {
  901. if (typeof callback === 'function') {
  902. callback(err, null);
  903. return;
  904. }
  905. return this.s.promiseLibrary.reject(err);
  906. }
  907. /**
  908. * An internal helper method. Do not invoke directly. Will be going away in the future
  909. *
  910. * @ignore
  911. * @method
  912. * @param {class} bulk either OrderedBulkOperation or UnorderdBulkOperation
  913. * @param {object} writeConcern
  914. * @param {object} options
  915. * @param {function} callback
  916. */
  917. bulkExecute(_writeConcern, options, callback) {
  918. if (typeof options === 'function') (callback = options), (options = {});
  919. options = options || {};
  920. if (typeof _writeConcern === 'function') {
  921. callback = _writeConcern;
  922. } else if (_writeConcern && typeof _writeConcern === 'object') {
  923. this.s.writeConcern = _writeConcern;
  924. }
  925. if (this.s.executed) {
  926. const executedError = toError('batch cannot be re-executed');
  927. return this._handleEarlyError(executedError, callback);
  928. }
  929. // If we have current batch
  930. if (this.isOrdered) {
  931. if (this.s.currentBatch) this.s.batches.push(this.s.currentBatch);
  932. } else {
  933. if (this.s.currentInsertBatch) this.s.batches.push(this.s.currentInsertBatch);
  934. if (this.s.currentUpdateBatch) this.s.batches.push(this.s.currentUpdateBatch);
  935. if (this.s.currentRemoveBatch) this.s.batches.push(this.s.currentRemoveBatch);
  936. }
  937. // If we have no operations in the bulk raise an error
  938. if (this.s.batches.length === 0) {
  939. const emptyBatchError = toError('Invalid Operation, no operations specified');
  940. return this._handleEarlyError(emptyBatchError, callback);
  941. }
  942. return { options, callback };
  943. }
  944. /**
  945. * The callback format for results
  946. * @callback BulkOperationBase~resultCallback
  947. * @param {MongoError} error An error instance representing the error during the execution.
  948. * @param {BulkWriteResult} result The bulk write result.
  949. */
  950. /**
  951. * Execute the bulk operation
  952. *
  953. * @method
  954. * @param {WriteConcern} [_writeConcern] Optional write concern. Can also be specified through options.
  955. * @param {object} [options] Optional settings.
  956. * @param {(number|string)} [options.w] The write concern.
  957. * @param {number} [options.wtimeout] The write concern timeout.
  958. * @param {boolean} [options.j=false] Specify a journal write concern.
  959. * @param {boolean} [options.fsync=false] Specify a file sync write concern.
  960. * @param {BulkOperationBase~resultCallback} [callback] A callback that will be invoked when bulkWrite finishes/errors
  961. * @throws {MongoError} Throws error if the bulk object has already been executed
  962. * @throws {MongoError} Throws error if the bulk object does not have any operations
  963. * @return {Promise|void} returns Promise if no callback passed
  964. */
  965. execute(_writeConcern, options, callback) {
  966. const ret = this.bulkExecute(_writeConcern, options, callback);
  967. if (!ret || isPromiseLike(ret)) {
  968. return ret;
  969. }
  970. options = ret.options;
  971. callback = ret.callback;
  972. return executeLegacyOperation(this.s.topology, executeCommands, [this, options, callback]);
  973. }
  974. /**
  975. * Handles final options before executing command
  976. *
  977. * An internal method. Do not invoke. Will not be accessible in the future
  978. *
  979. * @ignore
  980. * @param {object} config
  981. * @param {object} config.options
  982. * @param {number} config.batch
  983. * @param {function} config.resultHandler
  984. * @param {function} callback
  985. */
  986. finalOptionsHandler(config, callback) {
  987. const finalOptions = Object.assign({ ordered: this.isOrdered }, config.options);
  988. if (this.s.writeConcern != null) {
  989. finalOptions.writeConcern = this.s.writeConcern;
  990. }
  991. if (finalOptions.bypassDocumentValidation !== true) {
  992. delete finalOptions.bypassDocumentValidation;
  993. }
  994. // Set an operationIf if provided
  995. if (this.operationId) {
  996. config.resultHandler.operationId = this.operationId;
  997. }
  998. // Serialize functions
  999. if (this.s.options.serializeFunctions) {
  1000. finalOptions.serializeFunctions = true;
  1001. }
  1002. // Ignore undefined
  1003. if (this.s.options.ignoreUndefined) {
  1004. finalOptions.ignoreUndefined = true;
  1005. }
  1006. // Is the bypassDocumentValidation options specific
  1007. if (this.s.bypassDocumentValidation === true) {
  1008. finalOptions.bypassDocumentValidation = true;
  1009. }
  1010. // Is the checkKeys option disabled
  1011. if (this.s.checkKeys === false) {
  1012. finalOptions.checkKeys = false;
  1013. }
  1014. if (finalOptions.retryWrites) {
  1015. if (config.batch.batchType === UPDATE) {
  1016. finalOptions.retryWrites =
  1017. finalOptions.retryWrites && !config.batch.operations.some(op => op.multi);
  1018. }
  1019. if (config.batch.batchType === REMOVE) {
  1020. finalOptions.retryWrites =
  1021. finalOptions.retryWrites && !config.batch.operations.some(op => op.limit === 0);
  1022. }
  1023. }
  1024. try {
  1025. if (config.batch.batchType === INSERT) {
  1026. this.s.topology.insert(
  1027. this.s.namespace,
  1028. config.batch.operations,
  1029. finalOptions,
  1030. config.resultHandler
  1031. );
  1032. } else if (config.batch.batchType === UPDATE) {
  1033. this.s.topology.update(
  1034. this.s.namespace,
  1035. config.batch.operations,
  1036. finalOptions,
  1037. config.resultHandler
  1038. );
  1039. } else if (config.batch.batchType === REMOVE) {
  1040. this.s.topology.remove(
  1041. this.s.namespace,
  1042. config.batch.operations,
  1043. finalOptions,
  1044. config.resultHandler
  1045. );
  1046. }
  1047. } catch (err) {
  1048. // Force top level error
  1049. err.ok = 0;
  1050. // Merge top level error and return
  1051. handleCallback(callback, null, mergeBatchResults(config.batch, this.s.bulkResult, err, null));
  1052. }
  1053. }
  1054. /**
  1055. * Handles the write error before executing commands
  1056. *
  1057. * An internal helper method. Do not invoke directly. Will be going away in the future
  1058. *
  1059. * @ignore
  1060. * @param {function} callback
  1061. * @param {BulkWriteResult} writeResult
  1062. * @param {class} self either OrderedBulkOperation or UnorderdBulkOperation
  1063. */
  1064. handleWriteError(callback, writeResult) {
  1065. if (this.s.bulkResult.writeErrors.length > 0) {
  1066. if (this.s.bulkResult.writeErrors.length === 1) {
  1067. handleCallback(
  1068. callback,
  1069. new BulkWriteError(toError(this.s.bulkResult.writeErrors[0]), writeResult),
  1070. null
  1071. );
  1072. return true;
  1073. }
  1074. const msg = this.s.bulkResult.writeErrors[0].errmsg
  1075. ? this.s.bulkResult.writeErrors[0].errmsg
  1076. : 'write operation failed';
  1077. handleCallback(
  1078. callback,
  1079. new BulkWriteError(
  1080. toError({
  1081. message: msg,
  1082. code: this.s.bulkResult.writeErrors[0].code,
  1083. writeErrors: this.s.bulkResult.writeErrors
  1084. }),
  1085. writeResult
  1086. ),
  1087. null
  1088. );
  1089. return true;
  1090. } else if (writeResult.getWriteConcernError()) {
  1091. handleCallback(
  1092. callback,
  1093. new BulkWriteError(toError(writeResult.getWriteConcernError()), writeResult),
  1094. null
  1095. );
  1096. return true;
  1097. }
  1098. }
  1099. }
  1100. Object.defineProperty(BulkOperationBase.prototype, 'length', {
  1101. enumerable: true,
  1102. get: function() {
  1103. return this.s.currentIndex;
  1104. }
  1105. });
  1106. // Exports symbols
  1107. module.exports = {
  1108. Batch,
  1109. BulkOperationBase,
  1110. bson,
  1111. INSERT: INSERT,
  1112. UPDATE: UPDATE,
  1113. REMOVE: REMOVE,
  1114. BulkWriteError
  1115. };