问题
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!
回答1:
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 {*}
*/
回答2:
Use this:
var query = {
title: [
{ contains: "spanish"},
{contains: "2014"}
],
or: [
{title: { contains: "720p" } },
{title: { contains: "1080p" } },
]
};
Model.find(query).exec(function(err,items) {
....
});
来源:https://stackoverflow.com/questions/24461235/waterline-sails-js-and-conditions