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 #3057 #3537

Closed
wants to merge 2 commits into from
Closed
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
40 changes: 21 additions & 19 deletions packages/mongodb/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,30 @@ export class MongoDbAdapter<
}

/* TODO: Remove $out and $merge stages, else it returns an empty cursor. I think its safe to assume this is primarily for querying. */
async aggregateRaw(params: ServiceParams) {
const model = await this.getModel(params)
parsePipeline(params: ServiceParams) {
const pipeline = params.pipeline || []
const index = pipeline.findIndex((stage: Document) => stage.$feathers)
const before = index >= 0 ? pipeline.slice(0, index) : []
const feathersPipeline = this.makeFeathersPipeline(params)
const after = index >= 0 ? pipeline.slice(index + 1) : pipeline
return { before, after }
}

async aggregateRaw(params: ServiceParams) {
const model = await this.getModel(params)
const { before, after } = this.parsePipeline(params)
const feathersPipeline = this.makeFeathersPipeline(params)

return model.aggregate([...before, ...feathersPipeline, ...after], params.mongodb)
}

async countAggregate(params: ServiceParams) {
const model = await this.getModel(params)
const { before, after } = this.parsePipeline(params)

const res = await model.aggregate([...before, ...after, { $count: 'total' }], params.mongodb).toArray()
return res?.pop()?.totla ?? 0
}

makeFeathersPipeline(params: ServiceParams) {
const { filters, query } = this.filterQuery(null, params)
const pipeline: Document[] = [{ $match: query }]
Expand Down Expand Up @@ -210,21 +223,6 @@ export class MongoDbAdapter<
const { useEstimatedDocumentCount } = this.getOptions(params)
const { query } = this.filterQuery(null, params)

if (params.pipeline) {
const aggregateParams = {
...params,
query: {
...params.query,
$select: [this.id],
$sort: undefined,
$skip: undefined,
$limit: undefined
}
}
const result = await this.aggregateRaw(aggregateParams).then((result) => result.toArray())
return result.length
}

const model = await this.getModel(params)

if (useEstimatedDocumentCount && typeof model.estimatedDocumentCount === 'function') {
Expand Down Expand Up @@ -294,6 +292,10 @@ export class MongoDbAdapter<
return result.then((result) => result.toArray())
}

const getTotal = () => {
return params.pipeline ? this.countAggregate(params) : this.countDocuments(params)
}

if (paginationDisabled) {
if (filters.$limit === 0) {
return [] as Result[]
Expand All @@ -311,7 +313,7 @@ export class MongoDbAdapter<
}
}

const [data, total] = await Promise.all([getData(), this.countDocuments(params)])
const [data, total] = await Promise.all([getData(), getTotal()])

return {
total,
Expand Down