Waterline / Sails.js / Mongo How do I use AND conditions in a query for a single ticket?
My question is the same: Waterline (Sails.js): And conditions . There is an answer, but it doesn't work for me (nor for the questioner).
As far as I know, the waterline does not have the keyword 'and' in the query language. If I have multiple conditions for a query, I can simply use:
MyModel.find({fieldA:valueA, filedB:valueB});
But my question is how to do this:
MyModel.find({fieldA:{
contains:keyword1,
contains:keyword2
}});
I cannot set two keys with the 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 returns an empty set, and the third contains only keyword2. It looks like when you try to add two 'contains' conditions to the same file (fieldA), with a chained 'where' method, the second will overwrite the first.
I'm using mongoDB's own way to handle this as a workaround, Dose, does anyone know how to do this with a waterline? Or is it a problem that should be reported on github.
PS: sorry for my poor english: p
source to share
To answer your question, several are contains
not supported. Whether this is an oversight or a feature request is debatable. At least for the SQL case, it's a silly simple fix (although it 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 an 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.
source to share
Well I found the way this should be done for mongo database.
You can combine multiple criteria in one field with the key $and
:
orm.collections.user.find({
$and: [
{ name: { contains: 'neil' } },
{ name: { contains: 'armstrong' } }
]
});
The 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 {*}
*/
source to share