common.js 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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 its 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. _handleEarlyError(err, callback) {
  805. if (typeof callback === 'function') {
  806. callback(err, null);
  807. return;
  808. }
  809. return this.s.promiseLibrary.reject(err);
  810. }
  811. /**
  812. * Execute next write command in a chain
  813. *
  814. * @method
  815. * @param {class} bulk either OrderedBulkOperation or UnorderdBulkOperation
  816. * @param {object} writeConcern
  817. * @param {object} options
  818. * @param {function} callback
  819. */
  820. bulkExecute(_writeConcern, options, callback) {
  821. if (typeof options === 'function') (callback = options), (options = {});
  822. options = options || {};
  823. if (typeof _writeConcern === 'function') {
  824. callback = _writeConcern;
  825. } else if (_writeConcern && typeof _writeConcern === 'object') {
  826. this.s.writeConcern = _writeConcern;
  827. }
  828. if (this.s.executed) {
  829. const executedError = toError('batch cannot be re-executed');
  830. return this._handleEarlyError(executedError, callback);
  831. }
  832. // If we have current batch
  833. if (this.isOrdered) {
  834. if (this.s.currentBatch) this.s.batches.push(this.s.currentBatch);
  835. } else {
  836. if (this.s.currentInsertBatch) this.s.batches.push(this.s.currentInsertBatch);
  837. if (this.s.currentUpdateBatch) this.s.batches.push(this.s.currentUpdateBatch);
  838. if (this.s.currentRemoveBatch) this.s.batches.push(this.s.currentRemoveBatch);
  839. }
  840. // If we have no operations in the bulk raise an error
  841. if (this.s.batches.length === 0) {
  842. const emptyBatchError = toError('Invalid Operation, no operations specified');
  843. return this._handleEarlyError(emptyBatchError, callback);
  844. }
  845. return { options, callback };
  846. }
  847. /**
  848. * Handles final options before executing command
  849. *
  850. * @param {object} config
  851. * @param {object} config.options
  852. * @param {number} config.batch
  853. * @param {function} config.resultHandler
  854. * @param {function} callback
  855. */
  856. finalOptionsHandler(config, callback) {
  857. const finalOptions = Object.assign({ ordered: this.isOrdered }, config.options);
  858. if (this.s.writeConcern != null) {
  859. finalOptions.writeConcern = this.s.writeConcern;
  860. }
  861. if (finalOptions.bypassDocumentValidation !== true) {
  862. delete finalOptions.bypassDocumentValidation;
  863. }
  864. // Set an operationIf if provided
  865. if (this.operationId) {
  866. config.resultHandler.operationId = this.operationId;
  867. }
  868. // Serialize functions
  869. if (this.s.options.serializeFunctions) {
  870. finalOptions.serializeFunctions = true;
  871. }
  872. // Ignore undefined
  873. if (this.s.options.ignoreUndefined) {
  874. finalOptions.ignoreUndefined = true;
  875. }
  876. // Is the bypassDocumentValidation options specific
  877. if (this.s.bypassDocumentValidation === true) {
  878. finalOptions.bypassDocumentValidation = true;
  879. }
  880. // Is the checkKeys option disabled
  881. if (this.s.checkKeys === false) {
  882. finalOptions.checkKeys = false;
  883. }
  884. if (finalOptions.retryWrites) {
  885. if (config.batch.batchType === UPDATE) {
  886. finalOptions.retryWrites =
  887. finalOptions.retryWrites && !config.batch.operations.some(op => op.multi);
  888. }
  889. if (config.batch.batchType === REMOVE) {
  890. finalOptions.retryWrites =
  891. finalOptions.retryWrites && !config.batch.operations.some(op => op.limit === 0);
  892. }
  893. }
  894. try {
  895. if (config.batch.batchType === INSERT) {
  896. this.s.topology.insert(
  897. this.s.collection.namespace,
  898. config.batch.operations,
  899. finalOptions,
  900. config.resultHandler
  901. );
  902. } else if (config.batch.batchType === UPDATE) {
  903. this.s.topology.update(
  904. this.s.collection.namespace,
  905. config.batch.operations,
  906. finalOptions,
  907. config.resultHandler
  908. );
  909. } else if (config.batch.batchType === REMOVE) {
  910. this.s.topology.remove(
  911. this.s.collection.namespace,
  912. config.batch.operations,
  913. finalOptions,
  914. config.resultHandler
  915. );
  916. }
  917. } catch (err) {
  918. // Force top level error
  919. err.ok = 0;
  920. // Merge top level error and return
  921. handleCallback(
  922. callback,
  923. null,
  924. mergeBatchResults(false, config.batch, this.s.bulkResult, err, null)
  925. );
  926. }
  927. }
  928. /**
  929. * Handles the write error before executing commands
  930. *
  931. * @param {function} callback
  932. * @param {BulkWriteResult} writeResult
  933. * @param {class} self either OrderedBulkOperation or UnorderdBulkOperation
  934. */
  935. handleWriteError(callback, writeResult) {
  936. if (this.s.bulkResult.writeErrors.length > 0) {
  937. if (this.s.bulkResult.writeErrors.length === 1) {
  938. handleCallback(
  939. callback,
  940. new BulkWriteError(toError(this.s.bulkResult.writeErrors[0]), writeResult),
  941. null
  942. );
  943. return true;
  944. }
  945. handleCallback(
  946. callback,
  947. new BulkWriteError(
  948. toError({
  949. message: 'write operation failed',
  950. code: this.s.bulkResult.writeErrors[0].code,
  951. writeErrors: this.s.bulkResult.writeErrors
  952. }),
  953. writeResult
  954. ),
  955. null
  956. );
  957. return true;
  958. } else if (writeResult.getWriteConcernError()) {
  959. handleCallback(
  960. callback,
  961. new BulkWriteError(toError(writeResult.getWriteConcernError()), writeResult),
  962. null
  963. );
  964. return true;
  965. }
  966. }
  967. }
  968. Object.defineProperty(BulkOperationBase.prototype, 'length', {
  969. enumerable: true,
  970. get: function() {
  971. return this.s.currentIndex;
  972. }
  973. });
  974. // Exports symbols
  975. module.exports = {
  976. Batch,
  977. BulkOperationBase,
  978. BulkWriteError,
  979. BulkWriteResult,
  980. bson,
  981. FindOperators,
  982. handleMongoWriteConcernError,
  983. LegacyOp,
  984. mergeBatchResults,
  985. INVALID_BSON_ERROR: INVALID_BSON_ERROR,
  986. MULTIPLE_ERROR: MULTIPLE_ERROR,
  987. UNKNOWN_ERROR: UNKNOWN_ERROR,
  988. WRITE_CONCERN_ERROR: WRITE_CONCERN_ERROR,
  989. INSERT: INSERT,
  990. UPDATE: UPDATE,
  991. REMOVE: REMOVE,
  992. WriteError,
  993. WriteConcernError
  994. };