0

So I want to search for items in my database using mongoose .find() function. In my router I have this code to get certain items from the url. For example;

mydomainname.com/market?type=1&name=hi&stats=123 ...?type=123&name=&...

var type = req.query.type;
var name= req.query.name;
var stats= req.query.stats;
Model.find({type: type, name: name, stats: stats})
                .exec(function(err, model){
                    if(err){
                        console.log("error")
                    }else{
                        res.render('*jade*', {models: JSON.stringify(model)})
                    }})

This works fine, but when there is no query value in the url(as seen above) the query value will be set to ''. Which then sorts away every item I have on my database because there is none with exmaple name = '';

I have search around but I have not find any help so if any would be able to give me tip I would be grateful!

Wasiq Muhammad
  • 3,080
  • 3
  • 16
  • 29
DavidS
  • 11
  • 6
  • If you ask if the value is empty or null? This answer explains it: http://stackoverflow.com/a/16532000/3558900 – Randall Valenciano May 03 '16 at 16:46
  • Lets say I have 5 different querys and every of them can have no value. I can't see a method to use { $ne: null } where I don't use alot of if and else statements. And 2^5 is 32 different combination which makles alot of if and else statement. Can I somehow transfer { $ne: null } into a string object ? – DavidS May 03 '16 at 17:01
  • Possible dupe of http://stackoverflow.com/questions/31995166/mongoose-optional-search-query-parameters – JohnnyHK May 03 '16 at 17:02
  • Then build your query programmatically. If you don't want a lot of if else, you can do a for with an array and ask if the value is empty. http://stackoverflow.com/a/15673923/3558900 – Randall Valenciano May 03 '16 at 17:04

2 Answers2

0

You could create your find() query object based on the value of the request query parameters. The following example checks for the name field if it has an empty string value, remove the property from the query and then use the final query object as your find() filter:

var q = req.query;
if (q.name === '') {
    delete q.name;
}

// carry out further checks 

Model.find(q)
     .exec(function(err, model){
        if(err){
            console.log("error");
        }else{
            res.render('*jade*', {models: JSON.stringify(model)});
        }
    })
chridam
  • 100,957
  • 23
  • 236
  • 235
0
Try this



   var q = req.query;
    var data ={};
    if (q.name !=null) {
        data.name = q.name;
    }
    else if (q.type !=null){
        data.type = q.type;
    }
    else if (q.stats !=null){
       data.stats = q.stats;
    }
    else{
      data={};
    }
        Model.find(data)
             .exec(function(err, model){
                if(err){
                    console.log("error");
                }else{
                    res.render('*jade*', {models: JSON.stringify(model)});
                }
            })
Wasiq Muhammad
  • 3,080
  • 3
  • 16
  • 29