isSet query operator
We should add support for something like this:
{
where: {
someField: {
isSet: false
}
}
}
This would allow you to query for undefined fields using $isset in mongodb and NULL in SQL databases.
@kraman @bajtos @raymondfeng @fabien
Well if we can't use { where: { someField: null } }, then isSet makes sense.
We can use { where: { someField: null } } today but the semantics is different for NoSQL DBs. For example, in Mongo,
someField: null ==> someField: {$type: 10} someField: {isSet: false} ==> someField: {$exists: 10}
I'm fine to introduce connector specific extensions. Each connector should validate such extensions and report errors if a given operator or syntax is not supported.
We can use
{ where: { someField: null } }today but the semantics is different for NoSQL DBs.
Yeah, I was sort of expecting that, in wich case null is not a solution. Since LoopBack is promising to provide single API working on all databases, we need a solution that works consistently in all supported databases.
@bajtos @raymondfeng is there any timeframe when this feature makes the cut?
@marcellodesales not really. Can you contribute the feature yourself?
This already works using the exists method.
// REST Url
// /api/<modelName>?filter[where][<propertyName>][exists]=<true|false>
modelName.find({
where: {
propertyName: {
exists: true
}
}
}, callback)
The key is https://github.com/strongloop/loopback-connector-mongodb/blob/master/lib/mongodb.js#L488 where it will apply the $ sign to any property it doesn't understand as an else case.
@jsheely That is specific to mongodb. would be nice if this could be generalized to all dbs.
@kraman Agreed. I just wanted to be clear that this feature technically can work today. This and the related issues make it seem like it wasn't possible.
While it is not a baked part of the abstracted API it is very nice that it can fall down into the specific implementations of the provider and still be handled.
@kraman What's your expectation for relational DBs? Do you interpret exists: true as IS NOT NULL?
@raymondfeng yes, typically it would be IS NOT NULL
@jsheely I agree with that being available in the API, as well as with @kraman around "IS NOT NULL" on relational db...
@jsheely, the approach you mentioned doesn't work with current datasource-juggler when it coerces where clause: https://github.com/strongloop/loopback-datasource-juggler/blob/128665f1bd403b94a08a55a230a496e838c47347/lib/dao.js#L926
nothing is working for the existence of a field in a document.
+1. Having the same probem as @kblcuk
Isn't this a very common use case, I cant believe there is no solution ?
@pmoelgaard we've worked around it by introducing isDateSet property for model (defaulting to false since default for date in question is null), with which we can search for models which don't have date set.
This is suboptimal, though, and hopefully this issue will be eventually resolved.
This is redonk. High priority for my team +5
The automatic passthrough of unrecognised properties actually doesn't work for this on MongoDB.
The reason for this bug lies with this line
https://github.com/strongloop/loopback-datasource-juggler/blob/master/lib/dao.js#L1238
In my case, the property I am inspecting is of boolean type. My { exists: true/false } object will be converted to true by this line because DataType is the Boolean constructor.