Reputation: 3280
I'm using aggregate and match to grab data from the database. This is a simplified version:
Palette.aggregate([
{"$match": {
type: paletteType,
numberOfColors: {[colorNumberFilterTypeOperator]: colorNumber},
tags: paletteTag
}]);
Tags is an array.
I want to make it so if paletteTag is blank, it will return documents with anything in the tags field, but right now it returns none, as it's trying to match blank tags, which don't exist.
I could do an if statement to check the value of paletteTag and if it's blank set it to something that will match all tags, but I'm not sure what it would be. Doing something like /.*/g works to return anything with tags, but if the tag array is blank (equal to []), then it won't be returned. I want to return anything, whether it has tags in that field or not.
Upvotes: 0
Views: 141
Reputation: 4619
var match = {
type: paletteType,
numberOfColors: {[colorNumberFilterTypeOperator]: colorNumber}
}
paletteTag? match.tags = paletteTag : null; //add tag criteria only if palletTag exists
Palette.aggregate(
[{
"$match": match
}]);
Upvotes: 2