Reputation: 43
My question is same to Waterline (Sails.js): AND conditions. There is a answer but it's not work for me (neither for the questioner).
As far as I know , waterline doesn't have keyword 'and' in its query language. If I have multiple conditions to query, I can simply use:
MyModel.find({fieldA:valueA, filedB:valueB});
But my question is how to do:
MyModel.find({fieldA:{
contains:keyword1,
contains:keyword2
}});
For I can't set two key with same name in Object/JSON, so I tried :
MyModel.find({fieldA:
[
{contains:keyword1},
{contains:keyword2}
]
});
and
MyModel.find({fieldA:
{contains:[keyword1,keyword2]}
});
and
MyModel.find()
.where({fieldA:{contains:keyword1})
.where({fieldA:{contains:keyword2}});
But none of them work as expected : 1st and 2nd one returns empty set and 3rd one only contains keyword2. It appears that when you trying to add two 'contains' condition to a same filed(fieldA), with chained 'where' method, the second one will overwrite the first one.
I'm using the native mongoDB's way to handle this as a workaround, Dose anyone know how to do this in waterline's way ? Or is this a issue should be reported on github.
PS:sorry for my poor English :p
Upvotes: 4
Views: 3730
Reputation: 16027
Well I found the way it should be done for a mongo database.
You should be able to combine multiple criterias on the same field using the $and
key :
orm.collections.user.find({
$and: [
{ name: { contains: 'neil' } },
{ name: { contains: 'armstrong' } }
]
});
Sails-mongo source code shows this :
/**
* 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 {*}
*/
Upvotes: 4
Reputation: 988
To answer your question, multiple contains
are not supported. Whether it's an omission or a feature request is debatable. At least for an SQL case, it's a stupid simple fix (though took me over an hour to find it) to be able to support something like:
orm.collections.user.find({
and: [
{ name: { contains: 'neil' } },
{ name: { contains: 'armstrong' } }
]
});
All it needs is a case 'and':
in in the following file and it works:
https://github.com/balderdashy/waterline-sequel/blob/master/sequel/lib/criteriaProcessor.js#L120
I suspect the Mongo case would be similar.
Upvotes: 5