find_and_modify.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. 'use strict';
  2. const OperationBase = require('./operation').OperationBase;
  3. const applyRetryableWrites = require('../utils').applyRetryableWrites;
  4. const applyWriteConcern = require('../utils').applyWriteConcern;
  5. const decorateWithCollation = require('../utils').decorateWithCollation;
  6. const executeCommand = require('./db_ops').executeCommand;
  7. const formattedOrderClause = require('../utils').formattedOrderClause;
  8. const handleCallback = require('../utils').handleCallback;
  9. const ReadPreference = require('../core').ReadPreference;
  10. const maxWireVersion = require('../core/utils').maxWireVersion;
  11. const MongoError = require('../error').MongoError;
  12. const Aspect = require('./operation').Aspect;
  13. const defineAspects = require('./operation').defineAspects;
  14. const decorateWithExplain = require('../utils').decorateWithExplain;
  15. class FindAndModifyOperation extends OperationBase {
  16. constructor(collection, query, sort, doc, options) {
  17. super(options);
  18. this.collection = collection;
  19. this.query = query;
  20. this.sort = sort;
  21. this.doc = doc;
  22. }
  23. execute(callback) {
  24. const coll = this.collection;
  25. const query = this.query;
  26. const sort = formattedOrderClause(this.sort);
  27. const doc = this.doc;
  28. let options = this.options;
  29. // Create findAndModify command object
  30. let queryObject = {
  31. findAndModify: coll.collectionName,
  32. query: query
  33. };
  34. if (sort) {
  35. queryObject.sort = sort;
  36. }
  37. queryObject.new = options.new ? true : false;
  38. queryObject.remove = options.remove ? true : false;
  39. queryObject.upsert = options.upsert ? true : false;
  40. const projection = options.projection || options.fields;
  41. if (projection) {
  42. queryObject.fields = projection;
  43. }
  44. if (options.arrayFilters) {
  45. queryObject.arrayFilters = options.arrayFilters;
  46. }
  47. if (doc && !options.remove) {
  48. queryObject.update = doc;
  49. }
  50. if (options.maxTimeMS) queryObject.maxTimeMS = options.maxTimeMS;
  51. // Either use override on the function, or go back to default on either the collection
  52. // level or db
  53. options.serializeFunctions = options.serializeFunctions || coll.s.serializeFunctions;
  54. // No check on the documents
  55. options.checkKeys = false;
  56. // Final options for retryable writes and write concern
  57. options = applyRetryableWrites(options, coll.s.db);
  58. options = applyWriteConcern(options, { db: coll.s.db, collection: coll }, options);
  59. // Decorate the findAndModify command with the write Concern
  60. if (options.writeConcern) {
  61. queryObject.writeConcern = options.writeConcern;
  62. }
  63. // Have we specified bypassDocumentValidation
  64. if (options.bypassDocumentValidation === true) {
  65. queryObject.bypassDocumentValidation = options.bypassDocumentValidation;
  66. }
  67. options.readPreference = ReadPreference.primary;
  68. // Have we specified collation
  69. try {
  70. decorateWithCollation(queryObject, coll, options);
  71. } catch (err) {
  72. return callback(err, null);
  73. }
  74. if (options.hint) {
  75. // TODO: once this method becomes a CommandOperationV2 we will have the server
  76. // in place to check.
  77. const unacknowledgedWrite = options.writeConcern && options.writeConcern.w === 0;
  78. if (unacknowledgedWrite || maxWireVersion(coll.s.topology) < 8) {
  79. callback(
  80. new MongoError('The current topology does not support a hint on findAndModify commands')
  81. );
  82. return;
  83. }
  84. queryObject.hint = options.hint;
  85. }
  86. if (this.explain) {
  87. if (maxWireVersion(coll.s.topology) < 4) {
  88. callback(new MongoError(`server does not support explain on findAndModify`));
  89. return;
  90. }
  91. queryObject = decorateWithExplain(queryObject, this.explain);
  92. }
  93. // Execute the command
  94. executeCommand(coll.s.db, queryObject, options, (err, result) => {
  95. if (err) return handleCallback(callback, err, null);
  96. return handleCallback(callback, null, result);
  97. });
  98. }
  99. }
  100. defineAspects(FindAndModifyOperation, [Aspect.EXPLAINABLE]);
  101. module.exports = FindAndModifyOperation;