admin管理员组文章数量:1323200
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 user10366926user103669264 Answers
Reset to default 3You 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
本文标签:
版权声明:本文标题:javascript - Ignore undefined values that are passed in the query object parameter for Mongoose's Find function? - Stack 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742123181a2421816.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论