Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mongodb): Fix mongo count #3541

Merged
merged 5 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading