// Ниже мой метод, который возвратит агрегированный запрос:
aggregatedQuery = (filters) => {
const {
name
} = filters;
const aggregateStages = [
{
$match: {
$or: [
{ company: { $regex: name, $options: 'i' } },
],
},
}
]
return aggregateStages;
};
// Ниже приведен мой разбитый на страницы метод, который будет возвращать разбитую на страницы компанию, я использую async await для отправки данных моему внешнему интерфейсу.
exports.getPaginatedCompany = async (req,res) => {
const { page, quantity, filters } = req.body;
const aggregatedPaginatedCompany = await companyName.aggregatePaginate(
aggregatedQuery(filters), {
page: +page,
limit: +quantity,
},
)
.catch(err => res.status(httpStatusCode.INTERNAL_SERVER_ERROR).send({
message: err.message,
stackTrace: err.stack,
}));
res.status(httpStatusCode.OK).json(aggregatedPaginatedCompany);
}
// Ниже приведена моя схема, и я почти уверен, что следовал тому, что есть в документах, или я?
const mongoose = require('mongoose');
const mongooseAggregatePaginate = require('mongoose-aggregate-paginate');
const { Schema } = mongoose;
const CompanySchema = Schema(
{
company: {
type: String,
trim: true,
},
contactNum: {
type: [String],
require: true,
},
email: {
type: [String],
require: true,
},
},
{ timestamps: { createdAt: 'createdAt' } },
);
CompanySchema.plugin(mongooseAggregatePaginate);
module.exports = mongoose.model('companyName', CompanySchema);
Я провел всю ночь, пытаясь заставить этот код работать, я не понимаю, чего мне не хватает.