From e3373f9278c0191fff1d6e0fc272ee6f9ccd1936 Mon Sep 17 00:00:00 2001 From: Andrey Sobolev Date: Fri, 15 Mar 2024 00:12:09 +0700 Subject: [PATCH] UBERF-6014: Fix $faset usage (#4971) Signed-off-by: Andrey Sobolev --- .../src/inboxNotificationsClient.ts | 3 +- server/mongo/src/storage.ts | 38 +++++++++---------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/plugins/notification-resources/src/inboxNotificationsClient.ts b/plugins/notification-resources/src/inboxNotificationsClient.ts index b774223b8a..02cd062b82 100644 --- a/plugins/notification-resources/src/inboxNotificationsClient.ts +++ b/plugins/notification-resources/src/inboxNotificationsClient.ts @@ -124,7 +124,8 @@ export class InboxNotificationsClientImpl implements InboxNotificationsClient { }, lookup: { attachedTo: activity.class.ActivityMessage - } + }, + limit: 1000 } ) } diff --git a/server/mongo/src/storage.ts b/server/mongo/src/storage.ts index 917ba058e3..e71e163c65 100644 --- a/server/mongo/src/storage.ts +++ b/server/mongo/src/storage.ts @@ -430,7 +430,7 @@ abstract class MongoAdapterBase implements DbAdapter { domain?: Domain // Allow to find for Doc's in specified domain only. } ): Promise> { - const pipeline = [] + const pipeline: any[] = [] const match = { $match: this.translateQuery(clazz, query) } const slowPipeline = isLookupQuery(query) || isLookupSort(options?.sort) const steps = await this.getLookups(clazz, options?.lookup) @@ -440,14 +440,14 @@ abstract class MongoAdapterBase implements DbAdapter { } } pipeline.push(match) - const resultPipeline: any[] = [] + const totalPipeline: any[] = [...pipeline] await this.fillSortPipeline(clazz, options, pipeline) if (options?.limit !== undefined || typeof query._id === 'string') { - resultPipeline.push({ $limit: options?.limit ?? 1 }) + pipeline.push({ $limit: options?.limit ?? 1 }) } if (!slowPipeline) { for (const step of steps) { - resultPipeline.push({ $lookup: step }) + pipeline.push({ $lookup: step }) } } if (options?.projection !== undefined) { @@ -456,33 +456,24 @@ abstract class MongoAdapterBase implements DbAdapter { const ckey = this.checkMixinKey(key, clazz) as keyof T projection[ckey] = options.projection[key] } - resultPipeline.push({ $project: projection }) + pipeline.push({ $project: projection }) } else { - resultPipeline.push({ $project: { '%hash%': 0 } }) + pipeline.push({ $project: { '%hash%': 0 } }) } - pipeline.push({ - $facet: { - results: resultPipeline, - ...(options?.total === true ? { totalCount: [{ $count: 'count' }] } : {}) - } - }) + // const domain = this.hierarchy.getDomain(clazz) const domain = options?.domain ?? this.hierarchy.getDomain(clazz) const cursor = this.db.collection(domain).aggregate(pipeline, { checkKeys: false, enableUtf8Validation: false }) - const result: WithLookup[] = [] + let result: WithLookup[] = [] let total = options?.total === true ? 0 : -1 try { - const rres = await ctx.with('toArray', {}, async (ctx) => await this.toArray(cursor), { + result = (await ctx.with('toArray', {}, async (ctx) => await this.toArray(cursor), { domain, pipeline - }) - for (const r of rres) { - result.push(...r.results) - total = options?.total === true ? r.totalCount?.shift()?.count ?? 0 : -1 - } + })) as any[] } catch (e) { console.error('error during executing cursor in findWithPipeline', clazz, cutObjectArray(query), options, e) throw e @@ -493,6 +484,15 @@ abstract class MongoAdapterBase implements DbAdapter { }) this.clearExtraLookups(row) } + if (options?.total === true) { + totalPipeline.push({ $count: 'total' }) + const totalCursor = this.db.collection(domain).aggregate(totalPipeline, { + checkKeys: false, + enableUtf8Validation: false + }) + const arr = await this.toArray(totalCursor) + total = arr?.[0]?.total ?? 0 + } return toFindResult(this.stripHash(result), total) }