Suppose I have a model attribute tags: ['tag1', 'tag2', 'tag3']
. I want to find models that have a certain tag. How do I do it?
It appears contains does a string contains. Not array contains which I need?
You can make use of the $in
feature which allows you to filter from an array.
You can write the query like
Model.find({
}).where({
tags : {
$in : tagArray
}
}).exec(function(err, data) {
if(err) {
console.log(data);
} else {
console.log(data);
}
});
ModelName.find({tags: 'tag1'}).exec(function(err, tags){
if (err){
//handle error
}
//tags is array of all data containing tags = 'tag1'
console.log(tags);
});