find.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.FindOperation = void 0;
  4. const error_1 = require("../error");
  5. const read_concern_1 = require("../read_concern");
  6. const sort_1 = require("../sort");
  7. const utils_1 = require("../utils");
  8. const command_1 = require("./command");
  9. const operation_1 = require("./operation");
  10. /** @internal */
  11. class FindOperation extends command_1.CommandOperation {
  12. constructor(collection, ns, filter = {}, options = {}) {
  13. super(collection, options);
  14. this.options = { ...options };
  15. delete this.options.writeConcern;
  16. this.ns = ns;
  17. if (typeof filter !== 'object' || Array.isArray(filter)) {
  18. throw new error_1.MongoInvalidArgumentError('Query filter must be a plain object or ObjectId');
  19. }
  20. // special case passing in an ObjectId as a filter
  21. this.filter = filter != null && filter._bsontype === 'ObjectId' ? { _id: filter } : filter;
  22. }
  23. async execute(server, session) {
  24. this.server = server;
  25. const options = this.options;
  26. let findCommand = makeFindCommand(this.ns, this.filter, options);
  27. if (this.explain) {
  28. findCommand = (0, utils_1.decorateWithExplain)(findCommand, this.explain);
  29. }
  30. return server.commandAsync(this.ns, findCommand, {
  31. ...this.options,
  32. ...this.bsonOptions,
  33. documentsReturnedIn: 'firstBatch',
  34. session
  35. });
  36. }
  37. }
  38. exports.FindOperation = FindOperation;
  39. function makeFindCommand(ns, filter, options) {
  40. const findCommand = {
  41. find: ns.collection,
  42. filter
  43. };
  44. if (options.sort) {
  45. findCommand.sort = (0, sort_1.formatSort)(options.sort);
  46. }
  47. if (options.projection) {
  48. let projection = options.projection;
  49. if (projection && Array.isArray(projection)) {
  50. projection = projection.length
  51. ? projection.reduce((result, field) => {
  52. result[field] = 1;
  53. return result;
  54. }, {})
  55. : { _id: 1 };
  56. }
  57. findCommand.projection = projection;
  58. }
  59. if (options.hint) {
  60. findCommand.hint = (0, utils_1.normalizeHintField)(options.hint);
  61. }
  62. if (typeof options.skip === 'number') {
  63. findCommand.skip = options.skip;
  64. }
  65. if (typeof options.limit === 'number') {
  66. if (options.limit < 0) {
  67. findCommand.limit = -options.limit;
  68. findCommand.singleBatch = true;
  69. }
  70. else {
  71. findCommand.limit = options.limit;
  72. }
  73. }
  74. if (typeof options.batchSize === 'number') {
  75. if (options.batchSize < 0) {
  76. if (options.limit &&
  77. options.limit !== 0 &&
  78. Math.abs(options.batchSize) < Math.abs(options.limit)) {
  79. findCommand.limit = -options.batchSize;
  80. }
  81. findCommand.singleBatch = true;
  82. }
  83. else {
  84. findCommand.batchSize = options.batchSize;
  85. }
  86. }
  87. if (typeof options.singleBatch === 'boolean') {
  88. findCommand.singleBatch = options.singleBatch;
  89. }
  90. // we check for undefined specifically here to allow falsy values
  91. // eslint-disable-next-line no-restricted-syntax
  92. if (options.comment !== undefined) {
  93. findCommand.comment = options.comment;
  94. }
  95. if (typeof options.maxTimeMS === 'number') {
  96. findCommand.maxTimeMS = options.maxTimeMS;
  97. }
  98. const readConcern = read_concern_1.ReadConcern.fromOptions(options);
  99. if (readConcern) {
  100. findCommand.readConcern = readConcern.toJSON();
  101. }
  102. if (options.max) {
  103. findCommand.max = options.max;
  104. }
  105. if (options.min) {
  106. findCommand.min = options.min;
  107. }
  108. if (typeof options.returnKey === 'boolean') {
  109. findCommand.returnKey = options.returnKey;
  110. }
  111. if (typeof options.showRecordId === 'boolean') {
  112. findCommand.showRecordId = options.showRecordId;
  113. }
  114. if (typeof options.tailable === 'boolean') {
  115. findCommand.tailable = options.tailable;
  116. }
  117. if (typeof options.oplogReplay === 'boolean') {
  118. findCommand.oplogReplay = options.oplogReplay;
  119. }
  120. if (typeof options.timeout === 'boolean') {
  121. findCommand.noCursorTimeout = !options.timeout;
  122. }
  123. else if (typeof options.noCursorTimeout === 'boolean') {
  124. findCommand.noCursorTimeout = options.noCursorTimeout;
  125. }
  126. if (typeof options.awaitData === 'boolean') {
  127. findCommand.awaitData = options.awaitData;
  128. }
  129. if (typeof options.allowPartialResults === 'boolean') {
  130. findCommand.allowPartialResults = options.allowPartialResults;
  131. }
  132. if (options.collation) {
  133. findCommand.collation = options.collation;
  134. }
  135. if (typeof options.allowDiskUse === 'boolean') {
  136. findCommand.allowDiskUse = options.allowDiskUse;
  137. }
  138. if (options.let) {
  139. findCommand.let = options.let;
  140. }
  141. return findCommand;
  142. }
  143. (0, operation_1.defineAspects)(FindOperation, [
  144. operation_1.Aspect.READ_OPERATION,
  145. operation_1.Aspect.RETRYABLE,
  146. operation_1.Aspect.EXPLAINABLE,
  147. operation_1.Aspect.CURSOR_CREATING
  148. ]);
  149. //# sourceMappingURL=find.js.map