【发布时间】:2017-05-22 09:34:40
【问题描述】:
我正在尝试为我的 MongoDB 聚合构建动态 $match:
这是我希望我能做到的:
var matchStr = "";
if (req.body.propertyID) {
matchStr += "property: { $in: properties },"
}
if (req.body.status=="ACTIVE" || req.body.status=="NOPAY") {
matchStr += "status : {$lte: 4},"
} else if (req.body.status) {
matchStr += "status : {$lte: req.body.status},"
}
matchStr += "checkin : {$gte: (checkin)}, checkout: {$lte: (checkout)}"
这就是我最终要做的:
它太乱了,现在我必须在 $match 中添加更多语句,这将使我的 if 结构更大。
必须有更好的方法来做到这一点!
我希望有人知道如何在没有“if hell :)”的情况下动态构建它们
if (req.body.propertyID & req.body.status=="ALL") {
var matchStr = {
$match: {
$and:
[{
property: { $in: properties },
checkin : {$gte: (checkin)},
checkout: {$lte: (checkout)}
}]
}
}
} else if (!req.body.propertyID & !req.body.status) {
var matchStr = {
$match: {
$and:
[{
checkin : {$gte: (checkin)},
checkout: {$lte: (checkout)}
}]
}
}
} else if ((req.body.status=="ACTIVE" || req.body.status=="NOPAY")) {
if (req.body.propertyID) {
var matchStr = {
$match: {
$and:
[{
property: { $in: properties },
status : {$lte: 4},
checkin : {$gte: (checkin)},
checkout: {$lte: (checkout)}
}]
}
}
} else {
var matchStr = {
$match: {
$and:
[{
status : {$lte: 4},
checkin : {$gte: (checkin)},
checkout: {$lte: (checkout)}
}]
}
}
}
} else {
if (req.body.propertyID) {
var matchStr = {
$match: {
$and:
[{
property: { $in: properties },
status : {$lte: req.body.status},
checkin : {$gte: (checkin)},
checkout: {$lte: (checkout)}
}]
}
}
} else {
var matchStr = {
$match: {
$and:
[{
status : {$lte: req.body.status},
checkin : {$gte: (checkin)},
checkout: {$lte: (checkout)}
}]
}
}
}
}
更新! - 现在几乎明白了,除了它把“myMatch”放在 mongoDB 语句中。
这是新代码:
var myMatch = {
checkin: {$gte: checkin},
checkout: {$lte: checkout}
};
if (req.body.hasOwnProperty('propertyID')) {
var properties = req.body.propertyID.map(function (obj) {
return obj._id;
});
console.log("properties: " + properties);
myMatch["property"] = { "$in": properties };
}
if (req.body.status=="ACTIVE" || req.body.status=="NOPAY") {
myMatch["status"] = { "$lte": 4 };
} else if (req.body.hasOwnProperty('status')) {
myMatch["status"] = { "$lte": +req.body.status };
}
这是该结构的控制台日志:
myMatch: {
"checkin": {
"$gte": 1473571600
},
"checkout": {
"$lte": 1596163600
},
"property": {
"$in": [
"PAP-720",
"ATL-D406",
"WAT-606"
]
},
"status": {
"$lte": 3
}
}
这里是我注入 myMatch 对象的地方:
bookingTable.aggregate([
{
$match: {
$and:
[{
myMatch
}]
}
},
这是最后的 mongodb 语句:
Mongoose:
booking.aggregate([
{ '$match': { '$and': [ { myMatch: { checkin: { '$gte': 1473571600 }, checkout: { '$lte': 1596163600 }, property: { '$in': [ 'PAP-720', 'ATL-D406', 'WAT-606' ] }, status: { '$lte': 3 } } } ] } },
【问题讨论】:
-
对于初学者来说,
checkin和checkout在每种情况下都始终存在,因此您可以只创建一个基础对象,因为向 JavaScript 对象添加属性很简单。其次,你听说过switch吗?无论您做什么,都不要再从字符串的角度思考问题,而是要学习一些有关对象操作的知识。 -
这是一个结构更复杂的小例子,用几种不同的语言完成,但概念基本相同Generating a Structure for Aggregation
-
是的,我可以下移签入和结帐,我还查看了
$project管道的示例,但我不太确定我是否理解它。它有点过头了。但看起来您可以将多个对象放入匹配中,而不仅仅是一个大字符串,对吗? -
!叹息!好的,那么至少请回答我这个问题。您的
checkin和checkout都被上面的括号括起来。为什么?这些还不是数组吗?请说与$in一起使用的properties至少是一个数组。 -
@NeilLunn - 是的
properties是一个数组["AAA","BBB]等等......我不确定你所说的签入是什么意思。我应该这样做吗?checkin : {$gte: checkin}? (对不起,我还在努力学习这些结构是如何工作的:-))
标签: javascript node.js mongodb mongodb-query