首页 > 解决方案 > 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 });

标签: node.jsmongoose

解决方案


这可以是一个解决方案吗?如果不等于“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 });
        }
    });

推荐阅读