我已经设置了一个前挂钩,以便在存在搜索查询参数的情况下运行自定义Mongoose聚合查询以按客户详细信息查找订单,如...
module.exports = function(hook, next) {
if (hook.params.query && hook.params.query.search) {
let search = hook.params.query.search;
let regEx = new RegExp(".*" + search + ".*", "i");
delete hook.params.query.search;
const OrderModel = hook.app.service("orders").Model;
OrderModel.aggregate([
{
$lookup: {
from: "customeraddress",
localField: "shipping_address",
foreignField: "_id",
as: "shipping_address"
}
},
{
$lookup: {
from: "customeraddress",
localField: "billing_address",
foreignField: "_id",
as: "billing_address"
}
},
{
$match: {
$or: [
{
"shipping_address.first_name": regEx
},
{
"shipping_address.last_name": regEx
},
{
"shipping_address.email": regEx
},
{
"billing_address.first_name": regEx
},
{
"billing_address.last_name": regEx
},
{
"billing_address.email": regEx
}
]
}
}
]).then(orders => {
hook.result = orders;
next();
});
} else {
next();
}
};
在返回订单的意义上工作得很好,但问题是我失去了分页结果的属性,这通常是
{
"total": 2453,
"limit": 25,
"skip": 0,
"data": []
}
那么如何在保持相同的响应结构的同时进行这样的自定义查询。这是我自己需要做的事情,还是有其他方法可以做到。