1

I'm using mongodb with sails.js querying a model that contains the titles of torrent files. But I'm not able to perform a waterline query using the "AND" condition. I've already tried it in several ways but most of them return empty or simply, never return.

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

When adding 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 entries containing "2014" AND ("720p" OR "1080p"), some of them also contain "spanish" but I think it's just a coincidence.

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

Thanks!

Theadd
  • 38
  • 5

2 Answers2

3

There is a solution but it's really a mongo one :

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

Notice the $and key?

From sails-mongo 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 {*}
 */
Armel Larcier
  • 15,747
  • 7
  • 68
  • 89
1

Use this:

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

Model.find(query).exec(function(err,items) {
  ....
});
mdunisch
  • 3,627
  • 5
  • 25
  • 41
  • 1
    Thank you but it doesn't work, returns an empty set. – Theadd Jun 29 '14 at 00:40
  • Sorry for the delay, I didn't notice. You can see the results from db queries, using the method in my first post, [here](http://37.187.9.5:1337/hash/search?query=spanish). They are truncated. Just edit the "query" parameter in url to search for different terms. You can also use something like `2014 spanish 720p | 1080p` as argument, [link](http://37.187.9.5:1337/hash/search?query=1024%20spanish%20720p%20|%201080p), [sample output](http://pastebin.com/est2YbCN). – Theadd Jul 02 '14 at 14:20
  • I ended up using a text index on title field so everything has changed now, thanks anyway. – Theadd Jul 23 '14 at 07:54