node.js - Mongoose 查找设置,例如。如果位置是全部显示所有结果不是位置:“全部”
问题描述
我使用猫鼬有一段时间了,但我不知道如何使查找结果更容易。我使用快递,当位置为“全部”时,我想获得所有结果,而不是位置:“全部”。任何想法家伙我该怎么做?
Property.find({ 'Location.City': request.params.location }, function(err, allProperties) {
if (err) {
console.log(Date() + " - ERROR Occured: " + err);
}
else {
response.render("propertiesView", { properties: allProperties, propertiesCount: allProperties.length, moment: moment });
}
})
.skip(request.params.page * 10)
.limit(10)
.sort({ PostDate: -1 });
解决方案
这可以是一个解决方案吗?如果不等于“ALL”或者另一个比这个更容易,则推送到一个变量?
// Set Search settings
var searchSettings = {};
if (request.params.location != "ALL") {
var location = { 'Location.City': request.params.location };
searchSettings.push(location);
}
Property.find(searchSettings, function(err, allProperties) {
if (err) { console.log(Date() + " - ERROR Occured: " + err); }
else {
response.render("propertiesView", { properties: allProperties, propertiesCount: allProperties.length, moment: moment });
}
});
推荐阅读
- python - 在文件python中搜索用户名
- arrays - 从bash中的子数组中提取json字符串
- scala - 避免与 Future { 阻塞 {} } 发生死锁
- python - 使用 Kubernetes Python 客户端获取 Ingress 网关 IP 地址
- javascript - 如何在 url 中添加授权令牌
- c# - ASP.net Core:浏览器找不到控制器的动作
- flutter - 我正在开发一个测验应用程序。我想知道如何在不使用构造函数的情况下将分数变量传递到另一个页面
- amazon-web-services - 如何在 AWS lambda 中一次将用户添加到多个认知组
- css - 反应javascript css网格砌体响应问题
- javascript - 如何在方向上修复 mat-input 设计:rtl 而不更改 html dir 属性?