From 0f1deb642ad4a4585c17aa2bf4eaec8fd7152ca0 Mon Sep 17 00:00:00 2001 From: DaddyWarbucks Date: Thu, 10 Oct 2024 11:08:32 -0500 Subject: [PATCH 1/5] Fix pipeline count --- packages/mongodb/src/adapter.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/mongodb/src/adapter.ts b/packages/mongodb/src/adapter.ts index b640bf395d..7cb0a78292 100644 --- a/packages/mongodb/src/adapter.ts +++ b/packages/mongodb/src/adapter.ts @@ -213,6 +213,7 @@ export class MongoDbAdapter< if (params.pipeline) { const aggregateParams = { ...params, + paginate: false, query: { ...params.query, $select: [this.id], From e6595b8d3c2dc6fbc32084fb980f3f85805b75f5 Mon Sep 17 00:00:00 2001 From: DaddyWarbucks Date: Thu, 10 Oct 2024 11:32:50 -0500 Subject: [PATCH 2/5] Use pipeline $count, add test --- packages/mongodb/src/adapter.ts | 5 +++-- packages/mongodb/test/index.test.ts | 22 ++++++++++++---------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/packages/mongodb/src/adapter.ts b/packages/mongodb/src/adapter.ts index 7cb0a78292..9a1695b4d8 100644 --- a/packages/mongodb/src/adapter.ts +++ b/packages/mongodb/src/adapter.ts @@ -214,6 +214,7 @@ export class MongoDbAdapter< const aggregateParams = { ...params, paginate: false, + pipeline: [...params.pipeline, { $count: 'total' }], query: { ...params.query, $select: [this.id], @@ -222,8 +223,8 @@ export class MongoDbAdapter< $limit: undefined } } - const result = await this.aggregateRaw(aggregateParams).then((result) => result.toArray()) - return result.length + const [result] = await this.aggregateRaw(aggregateParams).then((result) => result.toArray()) + return result.total } const model = await this.getModel(params) diff --git a/packages/mongodb/test/index.test.ts b/packages/mongodb/test/index.test.ts index 7aec6e887d..99197101d0 100644 --- a/packages/mongodb/test/index.test.ts +++ b/packages/mongodb/test/index.test.ts @@ -83,11 +83,6 @@ const testSuite = adapterTests([ 'params.adapter + multi' ]) -const defaultPaginate = { - default: 10, - max: 50 -} - describe('Feathers MongoDB Service', () => { const personSchema = { $id: 'Person', @@ -573,12 +568,19 @@ describe('Feathers MongoDB Service', () => { it('can count documents with aggregation', async () => { const service = app.service('people') const paginateBefore = service.options.paginate - service.options.paginate = defaultPaginate - const query = { age: { $gte: 25 } } - const findResult = await app.service('people').find({ query }) - const aggregationResult = await app.service('people').find({ query, pipeline: [] }) - assert.deepStrictEqual(findResult.total, aggregationResult.total) + const test = async (paginate: any) => { + service.options.paginate = paginate + const query = { age: { $gte: 25 } } + const findResult = await app.service('people').find({ query }) + const aggregationResult = await app.service('people').find({ query, pipeline: [] }) + assert.deepStrictEqual(findResult.total, aggregationResult.total) + } + + await test({ default: 10, max: 50 }) + // There are 2 people with age >= 25. + // Test that aggregation works when results are less than default + await test({ default: 1, max: 50 }) service.options.paginate = paginateBefore }) From e126577a47081733d18adb11a96e0fce3a6d93e7 Mon Sep 17 00:00:00 2001 From: DaddyWarbucks Date: Thu, 10 Oct 2024 11:36:21 -0500 Subject: [PATCH 3/5] Rearrange pipeline --- packages/mongodb/src/adapter.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/mongodb/src/adapter.ts b/packages/mongodb/src/adapter.ts index 9a1695b4d8..f25c0ceebd 100644 --- a/packages/mongodb/src/adapter.ts +++ b/packages/mongodb/src/adapter.ts @@ -148,10 +148,6 @@ export class MongoDbAdapter< pipeline.push({ $sort: filters.$sort }) } - if (filters.$select !== undefined) { - pipeline.push({ $project: this.getProjection(filters.$select) }) - } - if (filters.$skip !== undefined) { pipeline.push({ $skip: filters.$skip }) } @@ -160,6 +156,10 @@ export class MongoDbAdapter< pipeline.push({ $limit: filters.$limit }) } + if (filters.$select !== undefined) { + pipeline.push({ $project: this.getProjection(filters.$select) }) + } + return pipeline } @@ -167,6 +167,7 @@ export class MongoDbAdapter< if (!select) { return undefined } + if (Array.isArray(select)) { if (!select.includes(this.id)) { select = [this.id, ...select] From 927f357232dfd8f7cb946db4e67f4725dbb27e4e Mon Sep 17 00:00:00 2001 From: DaddyWarbucks Date: Thu, 10 Oct 2024 11:36:27 -0500 Subject: [PATCH 4/5] Add missing test --- packages/mongodb/test/index.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/mongodb/test/index.test.ts b/packages/mongodb/test/index.test.ts index 99197101d0..5d3d2f6fc5 100644 --- a/packages/mongodb/test/index.test.ts +++ b/packages/mongodb/test/index.test.ts @@ -29,6 +29,7 @@ const testSuite = adapterTests([ '.remove + multi', '.remove + multi no pagination', '.remove + id + query id', + '.remove + NotFound', '.update', '.update + $select', '.update + id + query', From ab9985c833c10b93d522fd7123631f10d12d91b1 Mon Sep 17 00:00:00 2001 From: DaddyWarbucks Date: Thu, 10 Oct 2024 14:43:01 -0500 Subject: [PATCH 5/5] Handle ro results --- packages/mongodb/src/adapter.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/mongodb/src/adapter.ts b/packages/mongodb/src/adapter.ts index f25c0ceebd..2b4a2036a6 100644 --- a/packages/mongodb/src/adapter.ts +++ b/packages/mongodb/src/adapter.ts @@ -225,6 +225,9 @@ export class MongoDbAdapter< } } const [result] = await this.aggregateRaw(aggregateParams).then((result) => result.toArray()) + if (!result) { + return 0 + } return result.total }