Waterline (Sails.js): And conditions

I am using mongodb with sails.js requesting a model containing headers of torrent files. But I cannot complete the waterline request using the AND clause. I've already tried this in a couple of ways, but most of them return empty or just, never return.

eg. the database contains an entry with:   "title": "300 Rise Of An Empire 2014 720p BluRay x264-BLOW [Subs Spanish Latino] mkv"

Each time you add each query parameter, line by line:

var query = Hash.find();
query = query.where({ "title": { "contains": "spanish" } });
query = query.where({ "title": { "contains": "2014" } });
query = query.where({"or": [ { "title": { "contains": "720p" } }, { "title": { "contains": "1080p" } } ] });

      

It returns records containing "2014" AND ("720p" OR "1080p"), some of them also contain "Spanish", but I think that's just a coincidence.

How can I specify "Spanish" AND "2014" AND ("720p" OR "1080p)?

Thank!

+1


source to share


2 answers


There is a solution, but this is really one of the mongos:

query = query.where({"$and": [ { "title": { "contains": "720p" } }, { "title": { "contains": "1080p" } } ] });

      

Pay attention to the key $and

?



From the mongo sail source:

/**
 * Parse Clause
 *
 * <clause> ::= { <clause-pair>, ... }
 *
 * <clause-pair> ::= <field> : <expression>
 *                 | or|$or: [<clause>, ...]
 *                 | $or   : [<clause>, ...]
 *                 | $and  : [<clause>, ...]
 *                 | $nor  : [<clause>, ...]
 *                 | like  : { <field>: <expression>, ... }
 *
 * @api private
 *
 * @param original
 * @returns {*}
 */

      

+3


source


Use this:



var query = {
  title: [
     { contains: "spanish"},
     {contains: "2014"}
  ],
  or: [
     {title: { contains: "720p" } },
     {title: { contains: "1080p" } },
  ]
};

Model.find(query).exec(function(err,items) {
  ....
});

      

+1


source







All Articles