admin管理员组

文章数量:1323548

So:

Sound.find({
  what: req.query.what,
  where: req.query.where,
  date: req.query.date,
  hour: req.query.hour}, function(err, varToStoreResult, count) {
  //stuff
});

Say that only req.query.what has an actual value, "yes" for example, while the rest (req.query.where, req.query.date, etc.) are all undefined/empty. If I leave this code as is, it would look for docs which have what = req.query.what AND where = req.query.where, etc. but I would only want it to find docs which have their what value = to req.query.what, since the other fields are undefined/empty.

So:

Sound.find({
  what: req.query.what,
  where: req.query.where,
  date: req.query.date,
  hour: req.query.hour}, function(err, varToStoreResult, count) {
  //stuff
});

Say that only req.query.what has an actual value, "yes" for example, while the rest (req.query.where, req.query.date, etc.) are all undefined/empty. If I leave this code as is, it would look for docs which have what = req.query.what AND where = req.query.where, etc. but I would only want it to find docs which have their what value = to req.query.what, since the other fields are undefined/empty.

Share Improve this question asked Oct 31, 2018 at 20:25 user10366926user10366926
Add a ment  | 

4 Answers 4

Reset to default 3

You have to filter your res.query object from undefined/empty values first, and then pass it to find function. If you have just a couple of properties, you can use if statement:

const query = req.query;
const conditions = {};

if (query.what) {
  conditions.what = query.what;
}

if (query.where) {
  conditions.where = query.where;
}

....


Sound.find(conditions, function () {});

Or if there is a lot of properties you can iterate over them:

const query = req.query;
const conditions = Object.keys(query)
  .reduce((result, key) => {
    if (query[key]) {
      result[key] = query[key];
    }
    return result;
}, {}); 

Sound.find(conditions, function () {});

Also, I would not advise to remove properties from the actual res.query object - delete res.query.what - because you won't be able to use it in another middleware if you would like to.

May be a bit ugly, but works for me for filtering undefines. Just perform JSON.parse(JSON.stringify()) on your query like this:

JSON.parse(JSON.stringify({
  what: req.query.what,
  where: req.query.where,
  date: req.query.date,
  hour: req.query.hour
}))

You can use a simple reduce on Object.entries

const req = {query: {a: 1,b: undefined,c: 2}}

const query = Object.entries(req.query).reduce((a, [k, v]) => {
  if (v !== undefined) {
    a[k] = v;
  }
  return a
}, {});

console.log(query)

i hope i understand you well here,this is my personally method i use for these kind of query or somesort of filtering:

var query = {}; /*this is our object*/

if(req.query.sell) {  /* if request is valid and not null/undefined/empty*/

var sell = req.query.sell; /*we past the query with in a variable as a value*/

var Som = "firstname"; /*after that we define our mongodb parametre as a name */

 query[Somesort] = Som; /*and here we past our name and value of parameter to object*/

}
/*and we do this*/
   collection-name-here.find(query, function(err, model-name-here){
        if(err){
            console.log(err);
        } else {
        /*your code
    });

hope this would help you guys

本文标签: