MCPcopy Create free account
hub / github.com/mikro-orm/mikro-orm / _find

Method _find

packages/mongodb/src/MongoConnection.ts:208–277  ·  view source on GitHub ↗
(
    entityName: EntityName<T>,
    where: FilterQuery<T>,
    opts: MongoFindOptions<T> = {},
  )

Source from the content-addressed store, hash-verified

206 }
207
208 private async _find<T extends object>(
209 entityName: EntityName<T>,
210 where: FilterQuery<T>,
211 opts: MongoFindOptions<T> = {},
212 ): Promise<{ cursor: FindCursor<T>; query: string }> {
213 await this.ensureConnection();
214 const collection = this.getCollectionName(entityName);
215 const options: FindOptions & Abortable = opts.ctx ? { session: opts.ctx } : {};
216
217 if (opts.fields) {
218 options.projection = opts.fields.reduce((o, k) => Object.assign(o, { [k]: 1 }), {});
219 }
220
221 if (opts.collation) {
222 options.collation = opts.collation;
223 }
224
225 if (opts.indexHint != null) {
226 options.hint = opts.indexHint;
227 }
228
229 if (opts.maxTimeMS != null) {
230 options.maxTimeMS = opts.maxTimeMS;
231 }
232
233 if (opts.allowDiskUse != null) {
234 options.allowDiskUse = opts.allowDiskUse;
235 }
236 if (opts.chunkSize != null) {
237 options.batchSize = opts.chunkSize;
238 }
239
240 if (opts.signal) {
241 options.signal = opts.signal;
242 }
243
244 const resultSet = this.getCollection<T>(entityName).find(where as Filter<T>, options);
245 let query = `db.getCollection('${collection}').find(${this.logObject(where)}, ${this.logObject(options)})`;
246 const orderBy = Utils.asArray(opts.orderBy);
247
248 if (orderBy.length > 0) {
249 const orderByTuples: [string, SortDirection][] = [];
250 orderBy.forEach(o => {
251 Utils.keys(o).forEach(k => {
252 const direction = o[k] as SortDirection;
253 if (typeof direction === 'string') {
254 orderByTuples.push([k.toString(), direction.toUpperCase() === QueryOrder.ASC ? 1 : -1]);
255 } else {
256 orderByTuples.push([k.toString(), direction]);
257 }
258 });
259 });
260 if (orderByTuples.length > 0) {
261 query += `.sort(${this.logObject(orderByTuples)})`;
262 resultSet.sort(orderByTuples);
263 }
264 }
265

Callers

nothing calls this directly

Calls 12

getCollectionNameMethod · 0.95
getCollectionMethod · 0.95
logObjectMethod · 0.95
reduceMethod · 0.80
asArrayMethod · 0.80
pushMethod · 0.80
sortMethod · 0.80
assignMethod · 0.65
findMethod · 0.65
keysMethod · 0.45
toStringMethod · 0.45
limitMethod · 0.45

Tested by

no test coverage detected