事情有点失控,希望有人能帮忙。
我构建了一个带有地点列表的简单Web应用程序,用户可以选择多个过滤器(btn切换)来缩小项目范围。一旦他们单击搜索,我便用搜索参数查询我的MongoDB。我正在使用NodeJ,Express,body-parser。
我只是检查是否已设置参数。如果已设置,则将其添加到我的查找查询中。这非常适合简单查询。现在我正在尝试建立一个更复杂的查询,例如如果选择“ Jan”和“ Warm”:查找所有包含“ Jan”的地方作为time_of的一部分,并且month_avg_temp大于16。如果未选择time_of_year,请改用avg_temp。
我当前的解决方案似乎有效,但是当我选择“轻度”和“温暖”时,它仅返回最后一个而不是两者。我也愿意就如何简化我的查询构建过程提出建议,因为它会耗费很多if语句。
这是我的模特
var placeSchema = new mongoose.Schema({
location: String,
time_of_year: String,
level: String,
avg_temp: Number,
historic_data: [{
month: String,
month_number: Number,
data:{
month_avg_temp: Number
},
}]
})
module.exports = mongoose.model("Place", placeSchema)
这是我的html
<div class="btn-group-toggle btn-group" role="group" data-toggle="buttons">
<label class="btn btn-lg btn-grey-outline px-2">
<input type="checkbox" name="time_of_year" value="jan" autocomplete="off" id="jan">Jan
</label>
<label class="btn btn-lg btn-grey-outline px-2">
<input type="checkbox" name="time_of_year" value="feb" autocomplete="off" id="feb">Feb
</label>
<label class="btn btn-lg btn-grey-outline px-2">
<input type="checkbox" name="time_of_year" value="mar" autocomplete="off" id="mar">Mar
</label>
<label class="btn btn-lg btn-grey-outline px-2">
<input type="checkbox" name="time_of_year" value="apr" autocomplete="off" id="apr">Apr
</label>
<label class="btn btn-lg btn-grey-outline px-2">
<input type="checkbox" name="time_of_year" value="may" autocomplete="off" id="may">May
</label>
<label class="btn btn-lg btn-grey-outline px-2">
<input type="checkbox" name="time_of_year" value="jun" autocomplete="off" id="jun">Jun
</label>
</div>
<div class="btn-group-toggle mb-2 btn-group" role="group" data-toggle="buttons">
<label class="btn btn-lg btn-grey-outline">
<input type="checkbox" name="cold" value="true" autocomplete="off" id="cold">❄️ Cold
</label>
<label class="btn btn-lg btn-grey-outline">
<input type="checkbox" name="mild" value="true" autocomplete="off" id="mild">☁️ Mild
</label>
<label class="btn btn-lg btn-grey-outline">
<input type="checkbox" name="warm" value="true" autocomplete="off" id="warm">☀️ Warm
</label>
</div>
我的app.js
if(req.body.time_of_year && (req.body.warm || req.body.mild || req.body.cold)){
if(req.body.warm){
var array_number = moment(req.body.time_of_year, "MMM").format("M");
query.historic_data =
{"$elemMatch": { "month_number": array_number, "data.month_avg_temp": {$gt:16}}}
}
if (req.body.cold){
var array_number = moment(req.body.time_of_year, "MMM").format("M");
query.historic_data =
{"$elemMatch": { "month_number": array_number, "data.month_avg_temp": {$lt:10}}}
}
if(req.body.mild){
var array_number = moment(req.body.time_of_year, "MMM").format("M");
query.historic_data =
{"$elemMatch": { "month_number": array_number, "data.month_avg_temp": {$lt: 16, $gt:10}}}
}
} else {
if(req.body.warm){
query.avg_temp = { $gt:16 }
}
if(req.body.cold){
query.avg_temp = { $lt: 10 }
}
if(req.body.mild){
query.avg_temp = { $lt: 16, $gt:10 }
}
}
Place.find(query, function(err, places){
if(err){
console.log(err);
} else{
res.render("index", {places:places})
}
})