common.js 28 KB

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