common.js 38 KB

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