【发布时间】:2019-01-02 12:46:41
【问题描述】:
我的查询有问题,我传递的参数是凭证Id 和userId,我需要检查该用户是否超过limitedQuantityPerUser。如果是,请排除此优惠券。但是我在比较匹配字段时遇到问题,例如如果凭证 ID 是 'aaa',请检查 quantityBrought > limitedQuantityPerUser。
我试过了
vouchers.find({
'status': {
$in: ['OP', 'WG', 'LO', 'SO']
},
'details.userReachedLimitedQuantity': {
$ne: userId
},
}, fields, {
sort: {
'order': 1
}
}
但这会给我列表中 userId 不相等的所有结果。不完全是我需要的。使用聚合解决更好吗?
示例资源 JSON 文件
[{
"_id": "111",
"details": {
"limitedQuantityPerUser": "3",
"userReachedLimitedQuantity": [{
"userId": "aaa",
"quantityBrought": "2"
},
{
"userId": "bbb",
"quantityBrought": "1"
},
{
"userId": "ccc",
"quantityBrought": "3"
}
],
"status": "OP"
}
},
{
"_id": "222",
"details": {
"limitedQuantityPerUser": "2",
"userReachedLimitedQuantity": [{
"userId": "eee",
"quantityBrought": "2"
},
{
"userId": "bbb",
"quantityBrought": "1"
},
{
"userId": "vvv",
"quantityBrought": "2"
}
],
"status": "OP"
}
}
]
【问题讨论】:
标签: mongodb aggregation-framework