Skip to content

Commit

Permalink
fix(mongodb): Fix mongo count (#3541)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaddyWarbucks authored Oct 31, 2024
1 parent 221b92b commit 3e95c7d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
18 changes: 12 additions & 6 deletions packages/mongodb/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}
Expand All @@ -160,13 +156,18 @@ export class MongoDbAdapter<
pipeline.push({ $limit: filters.$limit })
}

if (filters.$select !== undefined) {
pipeline.push({ $project: this.getProjection(filters.$select) })
}

return pipeline
}

getProjection(select?: string[] | { [key: string]: number }) {
if (!select) {
return undefined
}

if (Array.isArray(select)) {
if (!select.includes(this.id)) {
select = [this.id, ...select]
Expand Down Expand Up @@ -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],
Expand All @@ -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)
Expand Down
23 changes: 13 additions & 10 deletions packages/mongodb/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -83,11 +84,6 @@ const testSuite = adapterTests([
'params.adapter + multi'
])

const defaultPaginate = {
default: 10,
max: 50
}

describe('Feathers MongoDB Service', () => {
const personSchema = {
$id: 'Person',
Expand Down Expand Up @@ -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
})
Expand Down

0 comments on commit 3e95c7d

Please sign in to comment.