-1

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?

Jiew Meng
  • 74,635
  • 166
  • 442
  • 756

2 Answers2

3

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);
      }
    });
Parth Vyas
  • 467
  • 4
  • 16
  • Does this only work for a specific adapter or database? I tried to use this in my tests using 'sails-memory' and it does not seem work. – Lando-L Dec 06 '17 at 12:55
0
   ModelName.find({tags: 'tag1'}).exec(function(err, tags){
        if (err){
           //handle error
           }
//tags is array of all data containing tags = 'tag1'
        console.log(tags);
        });
dkatavic
  • 196
  • 6