common.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  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[result.writeErrors[i].index],
  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://docs.mongodb.com/manual/reference/command/update/#update-command-u u documentation}
  521. * @param {object} [options.hint] An optional hint for query optimization. See the {@link https://docs.mongodb.com/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://docs.mongodb.com/manual/reference/command/update/#update-command-u u documentation}
  547. * @param {object} [options.hint] An optional hint for query optimization. See the {@link https://docs.mongodb.com/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. // If we have autoEncryption on, batch-splitting must be done on 2mb chunks, but single documents
  663. // over 2mb are still allowed
  664. const usingAutoEncryption = !!(topology.s.options && topology.s.options.autoEncrypter);
  665. const maxBsonObjectSize =
  666. isMaster && isMaster.maxBsonObjectSize ? isMaster.maxBsonObjectSize : 1024 * 1024 * 16;
  667. const maxBatchSizeBytes = usingAutoEncryption ? 1024 * 1024 * 2 : maxBsonObjectSize;
  668. const maxWriteBatchSize =
  669. isMaster && isMaster.maxWriteBatchSize ? isMaster.maxWriteBatchSize : 1000;
  670. // Calculates the largest possible size of an Array key, represented as a BSON string
  671. // element. This calculation:
  672. // 1 byte for BSON type
  673. // # of bytes = length of (string representation of (maxWriteBatchSize - 1))
  674. // + 1 bytes for null terminator
  675. const maxKeySize = (maxWriteBatchSize - 1).toString(10).length + 2;
  676. // Final options for retryable writes and write concern
  677. let finalOptions = Object.assign({}, options);
  678. finalOptions = applyRetryableWrites(finalOptions, collection.s.db);
  679. finalOptions = applyWriteConcern(finalOptions, { collection: collection }, options);
  680. const writeConcern = finalOptions.writeConcern;
  681. // Get the promiseLibrary
  682. const promiseLibrary = options.promiseLibrary || Promise;
  683. // Final results
  684. const bulkResult = {
  685. ok: 1,
  686. writeErrors: [],
  687. writeConcernErrors: [],
  688. insertedIds: [],
  689. nInserted: 0,
  690. nUpserted: 0,
  691. nMatched: 0,
  692. nModified: 0,
  693. nRemoved: 0,
  694. upserted: []
  695. };
  696. // Internal state
  697. this.s = {
  698. // Final result
  699. bulkResult: bulkResult,
  700. // Current batch state
  701. currentBatch: null,
  702. currentIndex: 0,
  703. // ordered specific
  704. currentBatchSize: 0,
  705. currentBatchSizeBytes: 0,
  706. // unordered specific
  707. currentInsertBatch: null,
  708. currentUpdateBatch: null,
  709. currentRemoveBatch: null,
  710. batches: [],
  711. // Write concern
  712. writeConcern: writeConcern,
  713. // Max batch size options
  714. maxBsonObjectSize,
  715. maxBatchSizeBytes,
  716. maxWriteBatchSize,
  717. maxKeySize,
  718. // Namespace
  719. namespace: namespace,
  720. // BSON
  721. bson: bson,
  722. // Topology
  723. topology: topology,
  724. // Options
  725. options: finalOptions,
  726. // Current operation
  727. currentOp: currentOp,
  728. // Executed
  729. executed: executed,
  730. // Collection
  731. collection: collection,
  732. // Promise Library
  733. promiseLibrary: promiseLibrary,
  734. // Fundamental error
  735. err: null,
  736. // check keys
  737. checkKeys: typeof options.checkKeys === 'boolean' ? options.checkKeys : true
  738. };
  739. // bypass Validation
  740. if (options.bypassDocumentValidation === true) {
  741. this.s.bypassDocumentValidation = true;
  742. }
  743. }
  744. /**
  745. * Add a single insert document to the bulk operation
  746. *
  747. * @param {object} document the document to insert
  748. * @throws {MongoError}
  749. * @return {BulkOperationBase} A reference to self
  750. *
  751. * @example
  752. * const bulkOp = collection.initializeOrderedBulkOp();
  753. * // Adds three inserts to the bulkOp.
  754. * bulkOp
  755. * .insert({ a: 1 })
  756. * .insert({ b: 2 })
  757. * .insert({ c: 3 });
  758. * await bulkOp.execute();
  759. */
  760. insert(document) {
  761. if (this.s.collection.s.db.options.forceServerObjectId !== true && document._id == null)
  762. document._id = new ObjectID();
  763. return this.s.options.addToOperationsList(this, INSERT, document);
  764. }
  765. /**
  766. * Builds a find operation for an update/updateOne/delete/deleteOne/replaceOne.
  767. * Returns a builder object used to complete the definition of the operation.
  768. *
  769. * @method
  770. * @param {object} selector The selector for the bulk operation. See {@link https://docs.mongodb.com/manual/reference/command/update/#update-command-q q documentation}
  771. * @throws {MongoError} if a selector is not specified
  772. * @return {FindOperators} A helper object with which the write operation can be defined.
  773. *
  774. * @example
  775. * const bulkOp = collection.initializeOrderedBulkOp();
  776. *
  777. * // Add an updateOne to the bulkOp
  778. * bulkOp.find({ a: 1 }).updateOne({ $set: { b: 2 } });
  779. *
  780. * // Add an updateMany to the bulkOp
  781. * bulkOp.find({ c: 3 }).update({ $set: { d: 4 } });
  782. *
  783. * // Add an upsert
  784. * bulkOp.find({ e: 5 }).upsert().updateOne({ $set: { f: 6 } });
  785. *
  786. * // Add a deletion
  787. * bulkOp.find({ g: 7 }).deleteOne();
  788. *
  789. * // Add a multi deletion
  790. * bulkOp.find({ h: 8 }).delete();
  791. *
  792. * // Add a replaceOne
  793. * bulkOp.find({ i: 9 }).replaceOne({ j: 10 });
  794. *
  795. * // Update using a pipeline (requires Mongodb 4.2 or higher)
  796. * bulk.find({ k: 11, y: { $exists: true }, z: { $exists: true } }).updateOne([
  797. * { $set: { total: { $sum: [ '$y', '$z' ] } } }
  798. * ]);
  799. *
  800. * // All of the ops will now be executed
  801. * await bulkOp.execute();
  802. */
  803. find(selector) {
  804. if (!selector) {
  805. throw toError('Bulk find operation must specify a selector');
  806. }
  807. // Save a current selector
  808. this.s.currentOp = {
  809. selector: selector
  810. };
  811. return new FindOperators(this);
  812. }
  813. /**
  814. * Specifies a raw operation to perform in the bulk write.
  815. *
  816. * @method
  817. * @param {object} op The raw operation to perform.
  818. * @param {object} [options.hint] An optional hint for query optimization. See the {@link https://docs.mongodb.com/manual/reference/command/update/#update-command-hint|update command} reference for more information.
  819. * @return {BulkOperationBase} A reference to self
  820. */
  821. raw(op) {
  822. const key = Object.keys(op)[0];
  823. // Set up the force server object id
  824. const forceServerObjectId =
  825. typeof this.s.options.forceServerObjectId === 'boolean'
  826. ? this.s.options.forceServerObjectId
  827. : this.s.collection.s.db.options.forceServerObjectId;
  828. // Update operations
  829. if (
  830. (op.updateOne && op.updateOne.q) ||
  831. (op.updateMany && op.updateMany.q) ||
  832. (op.replaceOne && op.replaceOne.q)
  833. ) {
  834. op[key].multi = op.updateOne || op.replaceOne ? false : true;
  835. return this.s.options.addToOperationsList(this, UPDATE, op[key]);
  836. }
  837. // Crud spec update format
  838. if (op.updateOne || op.updateMany || op.replaceOne) {
  839. const multi = op.updateOne || op.replaceOne ? false : true;
  840. const operation = {
  841. q: op[key].filter,
  842. u: op[key].update || op[key].replacement,
  843. multi: multi
  844. };
  845. if (op[key].hint) {
  846. operation.hint = op[key].hint;
  847. }
  848. if (this.isOrdered) {
  849. operation.upsert = op[key].upsert ? true : false;
  850. if (op.collation) operation.collation = op.collation;
  851. } else {
  852. if (op[key].upsert) operation.upsert = true;
  853. }
  854. if (op[key].arrayFilters) operation.arrayFilters = op[key].arrayFilters;
  855. return this.s.options.addToOperationsList(this, UPDATE, operation);
  856. }
  857. // Remove operations
  858. if (
  859. op.removeOne ||
  860. op.removeMany ||
  861. (op.deleteOne && op.deleteOne.q) ||
  862. (op.deleteMany && op.deleteMany.q)
  863. ) {
  864. op[key].limit = op.removeOne ? 1 : 0;
  865. return this.s.options.addToOperationsList(this, REMOVE, op[key]);
  866. }
  867. // Crud spec delete operations, less efficient
  868. if (op.deleteOne || op.deleteMany) {
  869. const limit = op.deleteOne ? 1 : 0;
  870. const operation = { q: op[key].filter, limit: limit };
  871. if (this.isOrdered) {
  872. if (op.collation) operation.collation = op.collation;
  873. }
  874. return this.s.options.addToOperationsList(this, REMOVE, operation);
  875. }
  876. // Insert operations
  877. if (op.insertOne && op.insertOne.document == null) {
  878. if (forceServerObjectId !== true && op.insertOne._id == null)
  879. op.insertOne._id = new ObjectID();
  880. return this.s.options.addToOperationsList(this, INSERT, op.insertOne);
  881. } else if (op.insertOne && op.insertOne.document) {
  882. if (forceServerObjectId !== true && op.insertOne.document._id == null)
  883. op.insertOne.document._id = new ObjectID();
  884. return this.s.options.addToOperationsList(this, INSERT, op.insertOne.document);
  885. }
  886. if (op.insertMany) {
  887. for (let i = 0; i < op.insertMany.length; i++) {
  888. if (forceServerObjectId !== true && op.insertMany[i]._id == null)
  889. op.insertMany[i]._id = new ObjectID();
  890. this.s.options.addToOperationsList(this, INSERT, op.insertMany[i]);
  891. }
  892. return;
  893. }
  894. // No valid type of operation
  895. throw toError(
  896. 'bulkWrite only supports insertOne, insertMany, updateOne, updateMany, removeOne, removeMany, deleteOne, deleteMany'
  897. );
  898. }
  899. /**
  900. * helper function to assist with promiseOrCallback behavior
  901. * @ignore
  902. * @param {*} err
  903. * @param {*} callback
  904. */
  905. _handleEarlyError(err, callback) {
  906. if (typeof callback === 'function') {
  907. callback(err, null);
  908. return;
  909. }
  910. return this.s.promiseLibrary.reject(err);
  911. }
  912. /**
  913. * An internal helper method. Do not invoke directly. Will be going away in the future
  914. *
  915. * @ignore
  916. * @method
  917. * @param {class} bulk either OrderedBulkOperation or UnorderdBulkOperation
  918. * @param {object} writeConcern
  919. * @param {object} options
  920. * @param {function} callback
  921. */
  922. bulkExecute(_writeConcern, options, callback) {
  923. if (typeof options === 'function') (callback = options), (options = {});
  924. options = options || {};
  925. if (typeof _writeConcern === 'function') {
  926. callback = _writeConcern;
  927. } else if (_writeConcern && typeof _writeConcern === 'object') {
  928. this.s.writeConcern = _writeConcern;
  929. }
  930. if (this.s.executed) {
  931. const executedError = toError('batch cannot be re-executed');
  932. return this._handleEarlyError(executedError, callback);
  933. }
  934. // If we have current batch
  935. if (this.isOrdered) {
  936. if (this.s.currentBatch) this.s.batches.push(this.s.currentBatch);
  937. } else {
  938. if (this.s.currentInsertBatch) this.s.batches.push(this.s.currentInsertBatch);
  939. if (this.s.currentUpdateBatch) this.s.batches.push(this.s.currentUpdateBatch);
  940. if (this.s.currentRemoveBatch) this.s.batches.push(this.s.currentRemoveBatch);
  941. }
  942. // If we have no operations in the bulk raise an error
  943. if (this.s.batches.length === 0) {
  944. const emptyBatchError = toError('Invalid Operation, no operations specified');
  945. return this._handleEarlyError(emptyBatchError, callback);
  946. }
  947. return { options, callback };
  948. }
  949. /**
  950. * The callback format for results
  951. * @callback BulkOperationBase~resultCallback
  952. * @param {MongoError} error An error instance representing the error during the execution.
  953. * @param {BulkWriteResult} result The bulk write result.
  954. */
  955. /**
  956. * Execute the bulk operation
  957. *
  958. * @method
  959. * @param {WriteConcern} [_writeConcern] Optional write concern. Can also be specified through options.
  960. * @param {object} [options] Optional settings.
  961. * @param {(number|string)} [options.w] The write concern.
  962. * @param {number} [options.wtimeout] The write concern timeout.
  963. * @param {boolean} [options.j=false] Specify a journal write concern.
  964. * @param {boolean} [options.fsync=false] Specify a file sync write concern.
  965. * @param {BulkOperationBase~resultCallback} [callback] A callback that will be invoked when bulkWrite finishes/errors
  966. * @throws {MongoError} Throws error if the bulk object has already been executed
  967. * @throws {MongoError} Throws error if the bulk object does not have any operations
  968. * @return {Promise|void} returns Promise if no callback passed
  969. */
  970. execute(_writeConcern, options, callback) {
  971. const ret = this.bulkExecute(_writeConcern, options, callback);
  972. if (!ret || isPromiseLike(ret)) {
  973. return ret;
  974. }
  975. options = ret.options;
  976. callback = ret.callback;
  977. return executeLegacyOperation(this.s.topology, executeCommands, [this, options, callback]);
  978. }
  979. /**
  980. * Handles final options before executing command
  981. *
  982. * An internal method. Do not invoke. Will not be accessible in the future
  983. *
  984. * @ignore
  985. * @param {object} config
  986. * @param {object} config.options
  987. * @param {number} config.batch
  988. * @param {function} config.resultHandler
  989. * @param {function} callback
  990. */
  991. finalOptionsHandler(config, callback) {
  992. const finalOptions = Object.assign({ ordered: this.isOrdered }, config.options);
  993. if (this.s.writeConcern != null) {
  994. finalOptions.writeConcern = this.s.writeConcern;
  995. }
  996. if (finalOptions.bypassDocumentValidation !== true) {
  997. delete finalOptions.bypassDocumentValidation;
  998. }
  999. // Set an operationIf if provided
  1000. if (this.operationId) {
  1001. config.resultHandler.operationId = this.operationId;
  1002. }
  1003. // Serialize functions
  1004. if (this.s.options.serializeFunctions) {
  1005. finalOptions.serializeFunctions = true;
  1006. }
  1007. // Ignore undefined
  1008. if (this.s.options.ignoreUndefined) {
  1009. finalOptions.ignoreUndefined = true;
  1010. }
  1011. // Is the bypassDocumentValidation options specific
  1012. if (this.s.bypassDocumentValidation === true) {
  1013. finalOptions.bypassDocumentValidation = true;
  1014. }
  1015. // Is the checkKeys option disabled
  1016. if (this.s.checkKeys === false) {
  1017. finalOptions.checkKeys = false;
  1018. }
  1019. if (finalOptions.retryWrites) {
  1020. if (config.batch.batchType === UPDATE) {
  1021. finalOptions.retryWrites =
  1022. finalOptions.retryWrites && !config.batch.operations.some(op => op.multi);
  1023. }
  1024. if (config.batch.batchType === REMOVE) {
  1025. finalOptions.retryWrites =
  1026. finalOptions.retryWrites && !config.batch.operations.some(op => op.limit === 0);
  1027. }
  1028. }
  1029. try {
  1030. if (config.batch.batchType === INSERT) {
  1031. this.s.topology.insert(
  1032. this.s.namespace,
  1033. config.batch.operations,
  1034. finalOptions,
  1035. config.resultHandler
  1036. );
  1037. } else if (config.batch.batchType === UPDATE) {
  1038. this.s.topology.update(
  1039. this.s.namespace,
  1040. config.batch.operations,
  1041. finalOptions,
  1042. config.resultHandler
  1043. );
  1044. } else if (config.batch.batchType === REMOVE) {
  1045. this.s.topology.remove(
  1046. this.s.namespace,
  1047. config.batch.operations,
  1048. finalOptions,
  1049. config.resultHandler
  1050. );
  1051. }
  1052. } catch (err) {
  1053. // Force top level error
  1054. err.ok = 0;
  1055. // Merge top level error and return
  1056. handleCallback(callback, null, mergeBatchResults(config.batch, this.s.bulkResult, err, null));
  1057. }
  1058. }
  1059. /**
  1060. * Handles the write error before executing commands
  1061. *
  1062. * An internal helper method. Do not invoke directly. Will be going away in the future
  1063. *
  1064. * @ignore
  1065. * @param {function} callback
  1066. * @param {BulkWriteResult} writeResult
  1067. * @param {class} self either OrderedBulkOperation or UnorderdBulkOperation
  1068. */
  1069. handleWriteError(callback, writeResult) {
  1070. if (this.s.bulkResult.writeErrors.length > 0) {
  1071. if (this.s.bulkResult.writeErrors.length === 1) {
  1072. handleCallback(
  1073. callback,
  1074. new BulkWriteError(toError(this.s.bulkResult.writeErrors[0]), writeResult),
  1075. null
  1076. );
  1077. return true;
  1078. }
  1079. const msg = this.s.bulkResult.writeErrors[0].errmsg
  1080. ? this.s.bulkResult.writeErrors[0].errmsg
  1081. : 'write operation failed';
  1082. handleCallback(
  1083. callback,
  1084. new BulkWriteError(
  1085. toError({
  1086. message: msg,
  1087. code: this.s.bulkResult.writeErrors[0].code,
  1088. writeErrors: this.s.bulkResult.writeErrors
  1089. }),
  1090. writeResult
  1091. ),
  1092. null
  1093. );
  1094. return true;
  1095. } else if (writeResult.getWriteConcernError()) {
  1096. handleCallback(
  1097. callback,
  1098. new BulkWriteError(toError(writeResult.getWriteConcernError()), writeResult),
  1099. null
  1100. );
  1101. return true;
  1102. }
  1103. }
  1104. }
  1105. Object.defineProperty(BulkOperationBase.prototype, 'length', {
  1106. enumerable: true,
  1107. get: function() {
  1108. return this.s.currentIndex;
  1109. }
  1110. });
  1111. // Exports symbols
  1112. module.exports = {
  1113. Batch,
  1114. BulkOperationBase,
  1115. bson,
  1116. INSERT: INSERT,
  1117. UPDATE: UPDATE,
  1118. REMOVE: REMOVE,
  1119. BulkWriteError
  1120. };