如何在mongodb中搜索多个参数以呈现不同的结果

时间:2019-09-06 16:00:06

标签: mongodb search

我正在尝试使用搜索表单搜索mongo数据库,以将结果显示到我的露营地/搜索路线。我已经成功添加了一个模糊搜索,可以搜索数据库的多个部分,但是,当我尝试使用下拉列表指定类别搜索时,它将破坏代码。

我尝试添加

工作功能

router.get("/search", function(req, res){
    var text = req.query.searchAll;
    const category = req.query.searchcategory;
    if(req.query.searchAll) {
        const regex = new RegExp(escapeRegex(req.query.searchAll), 'gi');



    Campground.find({ $or: [ {title: regex},  {body: regex}, {subject: regex}  ] }, function(err, allCampgrounds){
        if(err){
            console.log(err);

        } else {

            res.render("campgrounds/search", {campgrounds:allCampgrounds, text: text});
        }
    });




    } else {

        Campground.find({}, function(err, allCampgrounds){
        if(err){
            console.log(err);

        } else {

            res.render("campgrounds/search", {campgrounds:allCampgrounds, text:text});
        }
    });
}

新的损坏功能

router.get("/search", function(req, res){
    var text = req.query.searchAll;
    const category = req.query.searchcategory;
    if(req.query.searchAll) {
        const regex = new RegExp(escapeRegex(req.query.searchAll), 'gi');



    Campground.find({ $or: [ {title: regex},  {body: regex}, {subject: regex}  ] }, function(err, allCampgrounds){
        if(err){
            console.log(err);

        } else {

            res.render("campgrounds/search", {campgrounds:allCampgrounds, text: text});
        }
    });


        } else if(req.query.searchcategory) {

        Campground.find({category:category}, function(err, allCampgrounds){
        if(err){
            console.log(err);

        } else {

            res.render("campgrounds/search", {campgrounds:allCampgrounds, text:text});
        }
    });

    } else {

        Campground.find({}, function(err, allCampgrounds){
        if(err){
            console.log(err);

        } else {

            res.render("campgrounds/search", {campgrounds:allCampgrounds, text:text});
        }
    });
}

当我尝试用第二次加载的代码加载我的应用程序时,出现错误

module.exports =路由器;语法错误

0 个答案:

没有答案