Multiple parameter mongoose query

I am making a search with 3 params. Each term is space delimited. prefixes attach the terms to respective strings

Example:

searchtext: #sometag otherTag _someAncestor

?id=_someAncestor%3B&tags=%23sometag%3BotherTag

req.query.id='_someAncestor'
req.query.tags'=#sometag;othertag'

I want to make it so that null params are ignored, and whenever a param is passed it returns documents where the property associated contains all elements in the query array.

I am using the following code;

exports.some = function(req, res) {
console.log(req.query);//

if (req.query.id&&(typeof(req.query.id === 'string'))) {
    var splitIds = req.query.id.split(';');

    if (typeof(splitIds !== 'array')){
        var idArray=[];

        idArray.push(splitIds);
    }
}
if (req.query.tags&&(typeof(req.query.tags === 'string'))) {
    var splitTags = req.query.tags.split(';');

    if (typeof(splitTags !== 'array')){
        var tagArray=[];
        tagArray.push(splitTags);
    }
}
var tags = splitTags|| tagArray;
var ids = splitIds || idArray;
console.log(tags);//['#sometag','otherTag']
console.log(ids);//['_someAncestor]

Layer.find({}).where('_ancestors').in(ids).where('tags').in(tags).exec(function (err, layers) {
    console.log(layers);
    if (err) {
        return res.status(500).json({
            error: 'Cannot list the layers'
        });
    }
    res.json(layers);
});

};

Note: *

  • In Webstorm my .where() clauses are italicized, maybe that is related?
  • The req.query is formed properly and I am passing in([tags]) or .in([ids]) or both.
  • I think in gets documents that have a subset of the array, and I want it to match where params contain every element specified, maybe $all.

    500 error cannot list layers everytime.