我需要按价格范围记录,但查询在shell中工作正常但在nodejs中没有。这是查询:
db.model.aggregate([
{ $match: { make_id: make_id } },
{
$lookup: {
from: 'variants',
localField: '_id',
foreignField: 'model_id',
as: 'variant_list'
}
},
{
$unwind: {
path: '$variant_list',
preserveNullAndEmptyArrays: false
}
},
{$match: {status:1, $and:[{'variant_list.price': {$lte:Number(max_price)}}]}},
{$project: { make_id: 1, model_name: 1, variant_list: '$variant_list'}}
])
我已经检查了数据库中的价格。它像NumberInt(300000)
一样存储,在我的查询中我已经解析为整数类型,如=> max_price
至Number(max_price)
。
它给了我所有小于等于300000
的记录,但它也给出了小于400000
的记录。
但如果我没有使用aggregate framework
那么它会给我完美的结果:
db.model.find({status:1, $and:[{'variant_list.price': {$lte:Number(max_price)}}]})
我不知道为什么$lte
无法与aggregate
和$lookup
完美配合。
请帮助!!
更新了样本集和文档
MakeId = a123;
Models:
-----------------------------
mid | makeid | model |
-----------------------------
m1 | a123 | MDLi1 |
m2 | a123 | MDLi2 |
m3 | a123 | MDLi3 |
m4 | a123 | MDLi4 |
m5 | a123 | MDLi5 |
-----------------------------
Variants:
----------------------------------------
vid | modelid | variants | price |
----------------------------------------
v1 | m1 | VARi1 | 150000 |
v2 | m1 | VARi2 | 250000 |
v3 | m1 | VARi3 | 300000 |
v4 | m1 | VARi4 | 350000 |
v5 | m1 | VARi5 | 380000 |
v6 | m2 | VARi6 | 250000 |
v7 | m2 | VARi7 | 280000 |
v8 | m2 | VARi8 | 380000 |
v9 | m2 | VARi9 | 400000 |
v10 | m2 | VARi10 | 450000 |
----------------------------------------
我需要按价格范围的变体价格获取模型详细信息和变体列表between 0 to 300000
我的欲望结果是:
{
"data": [
{
"makeid": "a123",
"modelid": "m1",
"variant_list": [
{
"variant_id": "v1",
"variant_name": "VARi1",
"price": 150000
},
{
"variant_id": "v2",
"variant_name": "VARi2",
"price": 250000
},
{
"variant_id": "v3",
"variant_name": "VARi3",
"price": 300000
}
]
},
{
"makeid": "a123",
"modelid": "m2",
"variant_list": [
{
"variant_id": "v6",
"variant_name": "VARi6",
"price": 250000
},
{
"variant_id": "v7",
"variant_name": "VARi7",
"price": 280000
}
]
}
]
}