find.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.FindOperation = void 0;
  4. const shared_1 = require("../cmap/wire_protocol/shared");
  5. const error_1 = require("../error");
  6. const read_concern_1 = require("../read_concern");
  7. const sort_1 = require("../sort");
  8. const utils_1 = require("../utils");
  9. const command_1 = require("./command");
  10. const operation_1 = require("./operation");
  11. const SUPPORTS_WRITE_CONCERN_AND_COLLATION = 5;
  12. /** @internal */
  13. class FindOperation extends command_1.CommandOperation {
  14. constructor(collection, ns, filter = {}, options = {}) {
  15. super(collection, options);
  16. this.options = options;
  17. this.ns = ns;
  18. if (typeof filter !== 'object' || Array.isArray(filter)) {
  19. throw new error_1.MongoInvalidArgumentError('Query filter must be a plain object or ObjectId');
  20. }
  21. // If the filter is a buffer, validate that is a valid BSON document
  22. if (Buffer.isBuffer(filter)) {
  23. const objectSize = filter[0] | (filter[1] << 8) | (filter[2] << 16) | (filter[3] << 24);
  24. if (objectSize !== filter.length) {
  25. throw new error_1.MongoInvalidArgumentError(`Query filter raw message size does not match message header size [${filter.length}] != [${objectSize}]`);
  26. }
  27. }
  28. // special case passing in an ObjectId as a filter
  29. this.filter = filter != null && filter._bsontype === 'ObjectID' ? { _id: filter } : filter;
  30. }
  31. execute(server, session, callback) {
  32. this.server = server;
  33. const serverWireVersion = (0, utils_1.maxWireVersion)(server);
  34. const options = this.options;
  35. if (options.allowDiskUse != null && serverWireVersion < 4) {
  36. callback(new error_1.MongoCompatibilityError('Option "allowDiskUse" is not supported on MongoDB < 3.2'));
  37. return;
  38. }
  39. if (options.collation && serverWireVersion < SUPPORTS_WRITE_CONCERN_AND_COLLATION) {
  40. callback(new error_1.MongoCompatibilityError(`Server ${server.name}, which reports wire version ${serverWireVersion}, does not support collation`));
  41. return;
  42. }
  43. if (serverWireVersion < 4) {
  44. if (this.readConcern && this.readConcern.level !== 'local') {
  45. callback(new error_1.MongoCompatibilityError(`Server find command does not support a readConcern level of ${this.readConcern.level}`));
  46. return;
  47. }
  48. const findCommand = makeLegacyFindCommand(this.ns, this.filter, options);
  49. if ((0, shared_1.isSharded)(server) && this.readPreference) {
  50. findCommand.$readPreference = this.readPreference.toJSON();
  51. }
  52. server.query(this.ns, findCommand, {
  53. ...this.options,
  54. ...this.bsonOptions,
  55. documentsReturnedIn: 'firstBatch',
  56. readPreference: this.readPreference
  57. }, callback);
  58. return;
  59. }
  60. let findCommand = makeFindCommand(this.ns, this.filter, options);
  61. if (this.explain) {
  62. findCommand = (0, utils_1.decorateWithExplain)(findCommand, this.explain);
  63. }
  64. server.command(this.ns, findCommand, {
  65. ...this.options,
  66. ...this.bsonOptions,
  67. documentsReturnedIn: 'firstBatch',
  68. session
  69. }, callback);
  70. }
  71. }
  72. exports.FindOperation = FindOperation;
  73. function makeFindCommand(ns, filter, options) {
  74. const findCommand = {
  75. find: ns.collection,
  76. filter
  77. };
  78. if (options.sort) {
  79. findCommand.sort = (0, sort_1.formatSort)(options.sort);
  80. }
  81. if (options.projection) {
  82. let projection = options.projection;
  83. if (projection && Array.isArray(projection)) {
  84. projection = projection.length
  85. ? projection.reduce((result, field) => {
  86. result[field] = 1;
  87. return result;
  88. }, {})
  89. : { _id: 1 };
  90. }
  91. findCommand.projection = projection;
  92. }
  93. if (options.hint) {
  94. findCommand.hint = (0, utils_1.normalizeHintField)(options.hint);
  95. }
  96. if (typeof options.skip === 'number') {
  97. findCommand.skip = options.skip;
  98. }
  99. if (typeof options.limit === 'number') {
  100. if (options.limit < 0) {
  101. findCommand.limit = -options.limit;
  102. findCommand.singleBatch = true;
  103. }
  104. else {
  105. findCommand.limit = options.limit;
  106. }
  107. }
  108. if (typeof options.batchSize === 'number') {
  109. if (options.batchSize < 0) {
  110. if (options.limit &&
  111. options.limit !== 0 &&
  112. Math.abs(options.batchSize) < Math.abs(options.limit)) {
  113. findCommand.limit = -options.batchSize;
  114. }
  115. findCommand.singleBatch = true;
  116. }
  117. else {
  118. findCommand.batchSize = options.batchSize;
  119. }
  120. }
  121. if (typeof options.singleBatch === 'boolean') {
  122. findCommand.singleBatch = options.singleBatch;
  123. }
  124. if (options.comment) {
  125. findCommand.comment = options.comment;
  126. }
  127. if (typeof options.maxTimeMS === 'number') {
  128. findCommand.maxTimeMS = options.maxTimeMS;
  129. }
  130. const readConcern = read_concern_1.ReadConcern.fromOptions(options);
  131. if (readConcern) {
  132. findCommand.readConcern = readConcern.toJSON();
  133. }
  134. if (options.max) {
  135. findCommand.max = options.max;
  136. }
  137. if (options.min) {
  138. findCommand.min = options.min;
  139. }
  140. if (typeof options.returnKey === 'boolean') {
  141. findCommand.returnKey = options.returnKey;
  142. }
  143. if (typeof options.showRecordId === 'boolean') {
  144. findCommand.showRecordId = options.showRecordId;
  145. }
  146. if (typeof options.tailable === 'boolean') {
  147. findCommand.tailable = options.tailable;
  148. }
  149. if (typeof options.timeout === 'boolean') {
  150. findCommand.noCursorTimeout = !options.timeout;
  151. }
  152. else if (typeof options.noCursorTimeout === 'boolean') {
  153. findCommand.noCursorTimeout = options.noCursorTimeout;
  154. }
  155. if (typeof options.awaitData === 'boolean') {
  156. findCommand.awaitData = options.awaitData;
  157. }
  158. if (typeof options.allowPartialResults === 'boolean') {
  159. findCommand.allowPartialResults = options.allowPartialResults;
  160. }
  161. if (options.collation) {
  162. findCommand.collation = options.collation;
  163. }
  164. if (typeof options.allowDiskUse === 'boolean') {
  165. findCommand.allowDiskUse = options.allowDiskUse;
  166. }
  167. if (options.let) {
  168. findCommand.let = options.let;
  169. }
  170. return findCommand;
  171. }
  172. function makeLegacyFindCommand(ns, filter, options) {
  173. const findCommand = {
  174. $query: filter
  175. };
  176. if (options.sort) {
  177. findCommand.$orderby = (0, sort_1.formatSort)(options.sort);
  178. }
  179. if (options.hint) {
  180. findCommand.$hint = (0, utils_1.normalizeHintField)(options.hint);
  181. }
  182. if (typeof options.returnKey === 'boolean') {
  183. findCommand.$returnKey = options.returnKey;
  184. }
  185. if (options.max) {
  186. findCommand.$max = options.max;
  187. }
  188. if (options.min) {
  189. findCommand.$min = options.min;
  190. }
  191. if (typeof options.showRecordId === 'boolean') {
  192. findCommand.$showDiskLoc = options.showRecordId;
  193. }
  194. if (options.comment) {
  195. findCommand.$comment = options.comment;
  196. }
  197. if (typeof options.maxTimeMS === 'number') {
  198. findCommand.$maxTimeMS = options.maxTimeMS;
  199. }
  200. if (options.explain != null) {
  201. findCommand.$explain = true;
  202. }
  203. return findCommand;
  204. }
  205. (0, operation_1.defineAspects)(FindOperation, [
  206. operation_1.Aspect.READ_OPERATION,
  207. operation_1.Aspect.RETRYABLE,
  208. operation_1.Aspect.EXPLAINABLE,
  209. operation_1.Aspect.CURSOR_CREATING
  210. ]);
  211. //# sourceMappingURL=find.js.map