我试图通过不同的客户ID(CUSTID) 获取不同的记录相匹配的值指定的字段,分页
- 所有文件
- 分页(跳过和限制)
- 返回指定字段
变种订单= mongoose.model( '订单',orderSchema());
我最初的想法是使用mongoose db query,但你不能使用distinct
与skip and limit
为Distinct is a method that returns an "array", and therefore you cannot modify something that is not a "Cursor":
Order
.distinct('request.headers.custID')
.where('response.status.code').equals(200)
.limit(limit)
.skip(skip)
.exec(function (err, orders) {
callback({
data: orders
});
});
于是我想到要用Aggregate
,使用$group
得到不同的customerID记录,$match
返回所有唯一的客户ID记录的状态码为200,并且$project
包含我想要的字段:
Order.aggregate(
[
{
"$project" :
{
'request.headers.custID' : 1,
//other fields to include
}
},
{
"$match" :
{
"response.status.code" : 200
}
},
{
"$group": {
"_id": "$request.headers.custID"
}
},
{
"$skip": skip
},
{
"$limit": limit
}
],
function (err, order) {}
);
尽管这会返回一个空数组。如果我删除project
,则只有$request.headers.custID
字段返回,实际上我需要更多。
有什么想法?
超级有用布雷克斯。也为解释+1! – Growler