From 83849c2df2d68649e58e67ee6bd732e4f43eb97f Mon Sep 17 00:00:00 2001 From: yassinedorbozgithub Date: Sun, 6 Apr 2025 11:46:37 +0100 Subject: [PATCH] fix: enhance aggregation pipeline --- .../nlp/repositories/nlp-value.repository.ts | 36 +++++++------------ 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/api/src/nlp/repositories/nlp-value.repository.ts b/api/src/nlp/repositories/nlp-value.repository.ts index ab276904..bab94d7c 100644 --- a/api/src/nlp/repositories/nlp-value.repository.ts +++ b/api/src/nlp/repositories/nlp-value.repository.ts @@ -147,7 +147,6 @@ export class NlpValueRepository extends BaseRepository< ): Promise[]> { const pipeline: PipelineStage[] = [ { - // support filters $match: { ...rest, ...($and.length @@ -162,7 +161,6 @@ export class NlpValueRepository extends BaseRepository< : {}), }, }, - // support pageQuery { $skip: skip, }, @@ -174,42 +172,34 @@ export class NlpValueRepository extends BaseRepository< from: 'nlpsampleentities', localField: '_id', foreignField: 'value', - as: 'sampleEntities', + as: '_sampleEntities', }, }, { $unwind: { - path: '$sampleEntities', + path: '$_sampleEntities', preserveNullAndEmptyArrays: true, }, }, { $group: { _id: '$_id', - value: { $first: '$value' }, - expressions: { $first: '$expressions' }, - builtin: { $first: '$builtin' }, - metadata: { $first: '$metadata' }, - createdAt: { $first: '$createdAt' }, - updatedAt: { $first: '$updatedAt' }, - entity: { $first: '$entity' }, + _originalDoc: { + $first: { + $unsetField: { input: '$$ROOT', field: 'nlpSamplesCount' }, + }, + }, nlpSamplesCount: { - $sum: { $cond: [{ $ifNull: ['$sampleEntities', false] }, 1, 0] }, + $sum: { $cond: [{ $ifNull: ['$_sampleEntities', false] }, 1, 0] }, }, }, }, { - $project: { - id: '$_id', - _id: 0, - value: 1, - expressions: 1, - builtin: 1, - entity: 1, - metadata: 1, - createdAt: 1, - updatedAt: 1, - nlpSamplesCount: 1, + $replaceWith: { + $mergeObjects: [ + '$_originalDoc', + { nlpSamplesCount: '$nlpSamplesCount' }, + ], }, }, ...(format === Format.FULL