如何使用聚合框架将此 SQLite 查询转换为 MongoDB 查询
SQLite 查询:
"
FROM car
WHERE (@make is NULL OR @make = make)
AND (@model is NULL OR @model = model)
AND (@minPrice is NULL OR @minPrice <= price)
AND (@maxPrice is NULL OR @maxPrice >= price)
"
我尝试转换为 mongodb 聚合 Famework,一切都按预期运行
[
{
$match: {
$and: [
{ $or: [{ make: null }, { make: "BMW" }] },
{ $or: [{ model: null }, { model: "320" }] },
{ $or: [{ price: null }, { price: { $gte: 1000 } }] },
{ $or: [{ price: null }, { price: { $lte: 80000 } }] },
],
},
},
{ $project: { _id: 0, make: 1, model: 1, price: 1 } },
];
但是当不匹配“make”时,它不会返回任何内容
[
{
$match: {
$and: [
{ $or: [{ make: null }, { make: "asds" }] },
{ $or: [{ model: null }, { model: "320" }] },
{ $or: [{ price: null }, { price: { $gte: 1000 } }] },
{ $or: [{ price: null }, { price: { $lte: 80000 } }] },
],
},
},
{ $project: { _id: 0, make: 1, model: 1, price: 1 } },
];
蓝山帝景
相关分类