find_and_modify.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. class FindAndModifyOperation extends OperationBase {
  11. constructor(collection, query, sort, doc, options) {
  12. super(options);
  13. this.collection = collection;
  14. this.query = query;
  15. this.sort = sort;
  16. this.doc = doc;
  17. }
  18. execute(callback) {
  19. const coll = this.collection;
  20. const query = this.query;
  21. const sort = formattedOrderClause(this.sort);
  22. const doc = this.doc;
  23. let options = this.options;
  24. // Create findAndModify command object
  25. const queryObject = {
  26. findAndModify: coll.collectionName,
  27. query: query
  28. };
  29. if (sort) {
  30. queryObject.sort = sort;
  31. }
  32. queryObject.new = options.new ? true : false;
  33. queryObject.remove = options.remove ? true : false;
  34. queryObject.upsert = options.upsert ? true : false;
  35. const projection = options.projection || options.fields;
  36. if (projection) {
  37. queryObject.fields = projection;
  38. }
  39. if (options.arrayFilters) {
  40. queryObject.arrayFilters = options.arrayFilters;
  41. }
  42. if (doc && !options.remove) {
  43. queryObject.update = doc;
  44. }
  45. if (options.maxTimeMS) queryObject.maxTimeMS = options.maxTimeMS;
  46. // Either use override on the function, or go back to default on either the collection
  47. // level or db
  48. options.serializeFunctions = options.serializeFunctions || coll.s.serializeFunctions;
  49. // No check on the documents
  50. options.checkKeys = false;
  51. // Final options for retryable writes and write concern
  52. options = applyRetryableWrites(options, coll.s.db);
  53. options = applyWriteConcern(options, { db: coll.s.db, collection: coll }, options);
  54. // Decorate the findAndModify command with the write Concern
  55. if (options.writeConcern) {
  56. queryObject.writeConcern = options.writeConcern;
  57. }
  58. // Have we specified bypassDocumentValidation
  59. if (options.bypassDocumentValidation === true) {
  60. queryObject.bypassDocumentValidation = options.bypassDocumentValidation;
  61. }
  62. options.readPreference = ReadPreference.primary;
  63. // Have we specified collation
  64. try {
  65. decorateWithCollation(queryObject, coll, options);
  66. } catch (err) {
  67. return callback(err, null);
  68. }
  69. // Execute the command
  70. executeCommand(coll.s.db, queryObject, options, (err, result) => {
  71. if (err) return handleCallback(callback, err, null);
  72. return handleCallback(callback, null, result);
  73. });
  74. }
  75. }
  76. module.exports = FindAndModifyOperation;