From 3e95c7df6ae7de6a3a406dfb61dd044ea905456f Mon Sep 17 00:00:00 2001 From: Beau Shaw Date: Wed, 30 Oct 2024 21:34:47 -0500 Subject: [PATCH] fix(mongodb): Fix mongo count (#3541) --- packages/mongodb/src/adapter.ts | 18 ++++++++++++------ packages/mongodb/test/index.test.ts | 23 +++++++++++++---------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/packages/mongodb/src/adapter.ts b/packages/mongodb/src/adapter.ts index b640bf395d..2b4a2036a6 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] @@ -213,6 +214,8 @@ export class MongoDbAdapter< if (params.pipeline) { const aggregateParams = { ...params, + paginate: false, + pipeline: [...params.pipeline, { $count: 'total' }], query: { ...params.query, $select: [this.id], @@ -221,8 +224,11 @@ 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()) + if (!result) { + return 0 + } + 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..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', @@ -83,11 +84,6 @@ const testSuite = adapterTests([ 'params.adapter + multi' ]) -const defaultPaginate = { - default: 10, - max: 50 -} - describe('Feathers MongoDB Service', () => { const personSchema = { $id: 'Person', @@ -573,12 +569,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 })