loopback icon indicating copy to clipboard operation
loopback copied to clipboard

isSet query operator

Open ritch opened this issue 11 years ago • 40 comments

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.

ritch avatar Jan 14 '15 17:01 ritch

@kraman @bajtos @raymondfeng @fabien

ritch avatar Jan 14 '15 17:01 ritch

Well if we can't use { where: { someField: null } }, then isSet makes sense.

bajtos avatar Jan 14 '15 18:01 bajtos

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.

raymondfeng avatar Jan 14 '15 19:01 raymondfeng

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 avatar Jan 15 '15 12:01 bajtos

@bajtos @raymondfeng is there any timeframe when this feature makes the cut?

marcellodesales avatar Mar 23 '15 21:03 marcellodesales

@marcellodesales not really. Can you contribute the feature yourself?

bajtos avatar Mar 24 '15 09:03 bajtos

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 avatar Mar 24 '15 15:03 jsheely

@jsheely That is specific to mongodb. would be nice if this could be generalized to all dbs.

kraman avatar Mar 24 '15 15:03 kraman

@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.

jsheely avatar Mar 24 '15 16:03 jsheely

@kraman What's your expectation for relational DBs? Do you interpret exists: true as IS NOT NULL?

raymondfeng avatar Mar 24 '15 16:03 raymondfeng

@raymondfeng yes, typically it would be IS NOT NULL

kraman avatar Mar 24 '15 17:03 kraman

@jsheely I agree with that being available in the API, as well as with @kraman around "IS NOT NULL" on relational db...

marcellodesales avatar Mar 27 '15 17:03 marcellodesales

@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

kblcuk avatar May 11 '15 14:05 kblcuk

nothing is working for the existence of a field in a document.

dhirenderbp avatar Jun 26 '15 08:06 dhirenderbp

+1. Having the same probem as @kblcuk

arealmaas avatar Sep 03 '15 09:09 arealmaas

Isn't this a very common use case, I cant believe there is no solution ?

pmoelgaard avatar Sep 07 '15 07:09 pmoelgaard

@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.

kblcuk avatar Sep 07 '15 08:09 kblcuk

This is redonk. High priority for my team +5

rawrmaan avatar Nov 11 '15 02:11 rawrmaan

The automatic passthrough of unrecognised properties actually doesn't work for this on MongoDB.

gausie avatar Nov 23 '15 13:11 gausie

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.

gausie avatar Nov 23 '15 14:11 gausie