diff --git a/packages/core/src/storage.ts b/packages/core/src/storage.ts index becca9f244..d44eaf6236 100644 --- a/packages/core/src/storage.ts +++ b/packages/core/src/storage.ts @@ -105,6 +105,13 @@ export interface ReverseLookup { */ export type Lookup<T extends Doc> = Refs<T> | ReverseLookups | (Refs<T> & ReverseLookups) +/** + * @public + */ +export type Projection<T extends Doc> = { + [P in keyof T]?: 0 | 1 +} + /** * @public */ @@ -113,9 +120,7 @@ export type FindOptions<T extends Doc> = { limit?: number sort?: SortingQuery<T> lookup?: Lookup<T> - projection?: { - [P in keyof T]?: 0 | 1 - } + projection?: Projection<T> } /** diff --git a/server/mongo/src/storage.ts b/server/mongo/src/storage.ts index f32e4483e9..bf393521f8 100644 --- a/server/mongo/src/storage.ts +++ b/server/mongo/src/storage.ts @@ -29,6 +29,7 @@ import core, { Lookup, Mixin, ModelDb, + Projection, QueryUpdate, Ref, ReverseLookups, @@ -349,7 +350,12 @@ abstract class MongoAdapterBase extends TxProcessor { } } if (options?.projection !== undefined) { - resultPipeline.push({ $project: options.projection }) + const projection: Projection<T> = {} + for (const key in options.projection) { + const ckey = this.checkMixinKey<T>(key, clazz) as keyof T + projection[ckey] = options.projection[key] + } + resultPipeline.push({ $project: projection }) } pipeline.push({ $facet: { @@ -438,7 +444,12 @@ abstract class MongoAdapterBase extends TxProcessor { let cursor = coll.find<T>(this.translateQuery(_class, query)) if (options?.projection !== undefined) { - cursor = cursor.project(options.projection) + const projection: Projection<T> = {} + for (const key in options.projection) { + const ckey = this.checkMixinKey<T>(key, _class) as keyof T + projection[ckey] = options.projection[key] + } + cursor = cursor.project(projection) } let total: number | undefined if (options !== null && options !== undefined) {