我试图使用基于某些参数的mongoose动态创建查询
get<F extends Filterable>(filter: F, projections: { [fieldName: string]: 0 | 1 }, toEntity: (document: D) => Promise<T>) {
const pagingFilter = { ...filter.page }
const sortingFilter = { ...filter.sort }
const dataFilter: any = omit(filter, ["page"])
Object.keys(dataFilter).map((key) => {
if (isUndefined(dataFilter[key])) {
delete dataFilter[key]
}
})
let query = this.model.find(dataFilter, projections)
if (!isUndefined(pagingFilter.limit) && !isUndefined(pagingFilter.offset)) {
query = query.limit(pagingFilter.limit).skip(pagingFilter.limit * pagingFilter.offset)
}
if (!isUndefined(sortingFilter.name) && !isUndefined(sortingFilter.order)) {
query = query.sort({ [sortingFilter.name]: sortingFilter.order })
}
return Promise.all([
this.model.count(dataFilter),
query.exec()
])
.then(([total, values]) => ({ total, values }))
}
如果我传递了sort参数,我应该收到已排序的参数,页面参数也一样,但对我不起作用
有一种在mongoose中撰写查询的方法吗?