fix: enhance aggregation pipeline

This commit is contained in:
yassinedorbozgithub 2025-04-06 11:46:37 +01:00
parent 903ff0bbf9
commit 83849c2df2

View File

@ -147,7 +147,6 @@ export class NlpValueRepository extends BaseRepository<
): Promise<TNlpValueCount<F>[]> { ): Promise<TNlpValueCount<F>[]> {
const pipeline: PipelineStage[] = [ const pipeline: PipelineStage[] = [
{ {
// support filters
$match: { $match: {
...rest, ...rest,
...($and.length ...($and.length
@ -162,7 +161,6 @@ export class NlpValueRepository extends BaseRepository<
: {}), : {}),
}, },
}, },
// support pageQuery
{ {
$skip: skip, $skip: skip,
}, },
@ -174,42 +172,34 @@ export class NlpValueRepository extends BaseRepository<
from: 'nlpsampleentities', from: 'nlpsampleentities',
localField: '_id', localField: '_id',
foreignField: 'value', foreignField: 'value',
as: 'sampleEntities', as: '_sampleEntities',
}, },
}, },
{ {
$unwind: { $unwind: {
path: '$sampleEntities', path: '$_sampleEntities',
preserveNullAndEmptyArrays: true, preserveNullAndEmptyArrays: true,
}, },
}, },
{ {
$group: { $group: {
_id: '$_id', _id: '$_id',
value: { $first: '$value' }, _originalDoc: {
expressions: { $first: '$expressions' }, $first: {
builtin: { $first: '$builtin' }, $unsetField: { input: '$$ROOT', field: 'nlpSamplesCount' },
metadata: { $first: '$metadata' }, },
createdAt: { $first: '$createdAt' }, },
updatedAt: { $first: '$updatedAt' },
entity: { $first: '$entity' },
nlpSamplesCount: { nlpSamplesCount: {
$sum: { $cond: [{ $ifNull: ['$sampleEntities', false] }, 1, 0] }, $sum: { $cond: [{ $ifNull: ['$_sampleEntities', false] }, 1, 0] },
}, },
}, },
}, },
{ {
$project: { $replaceWith: {
id: '$_id', $mergeObjects: [
_id: 0, '$_originalDoc',
value: 1, { nlpSamplesCount: '$nlpSamplesCount' },
expressions: 1, ],
builtin: 1,
entity: 1,
metadata: 1,
createdAt: 1,
updatedAt: 1,
nlpSamplesCount: 1,
}, },
}, },
...(format === Format.FULL ...(format === Format.FULL