find_one_and_replace.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict';
  2. const MongoError = require('../core').MongoError;
  3. const FindAndModifyOperation = require('./find_and_modify');
  4. const hasAtomicOperators = require('../utils').hasAtomicOperators;
  5. class FindOneAndReplaceOperation extends FindAndModifyOperation {
  6. constructor(collection, filter, replacement, options) {
  7. if ('returnDocument' in options && 'returnOriginal' in options) {
  8. throw new MongoError(
  9. 'findOneAndReplace option returnOriginal is deprecated in favor of returnDocument and cannot be combined'
  10. );
  11. }
  12. // Final options
  13. const finalOptions = Object.assign({}, options);
  14. finalOptions.fields = options.projection;
  15. finalOptions.update = true;
  16. finalOptions.new = options.returnDocument === 'after' || options.returnOriginal === false;
  17. finalOptions.upsert = options.upsert === true;
  18. if (filter == null || typeof filter !== 'object') {
  19. throw new TypeError('Filter parameter must be an object');
  20. }
  21. if (replacement == null || typeof replacement !== 'object') {
  22. throw new TypeError('Replacement parameter must be an object');
  23. }
  24. if (hasAtomicOperators(replacement)) {
  25. throw new TypeError('Replacement document must not contain atomic operators');
  26. }
  27. super(collection, filter, finalOptions.sort, replacement, finalOptions);
  28. }
  29. }
  30. module.exports = FindOneAndReplaceOperation;