8

I'm using this query to verify if data exists on my Firebase (using AngularFire2)

let aux = this.afData.list("/drivers", { query: { orderByChild: '/accountHistory/approved', equalTo: null } });

Works pretty fine, but i also need to make an inverse query.

Like this

let aux = this.afData.list("/drivers", { query: { orderByChild: '/accountHistory/approved', equalTo: !null } });

The problem is. This second query, only returns if the value is TRUE, i'm storing a TIMESTAMP on /driveruid/accountHistory/approved'

There is any way to only verify if the Value exist or doesn't exist?

Thanks!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Possible duplicate of [is it possible query data that are not equal to the specified condition?](https://stackoverflow.com/questions/28582223/is-it-possible-query-data-that-are-not-equal-to-the-specified-condition) – Roman C Jul 03 '17 at 18:36
  • There is no *any value* check. But what values do you store in there? You might be able to get somewhere with `startAt("a").endAt("z").limitToFirst(1)` (depending on the range of values you can have). – Frank van Puffelen Jul 04 '17 at 00:00
  • @FrankvanPuffelen I appreciate the help! My question had only 2 cases. * The Absence of data * The existence of a TIMESTAMP Rohan's suggestion worked very well! Thanks Again! – Igor Santos de Lima Jul 04 '17 at 11:13

1 Answers1

11

From the Firebase docs, queries with orderByChild return lists in the following order :-

Children with a null value for the specified child key come first.

Children with a value of false for the specified child key come next. If multiple children have a value of false, they are sorted lexicographically by key.

Children with a value of true for the specified child key come next. If multiple children have a value of true, they are sorted lexicographically by key.

Children with a numeric value come next, sorted in ascending order. If multiple children have the same numerical value for the specified child node, they are sorted by key.

Strings come after numbers and are sorted lexicographically in ascending order. If multiple children have the same value for the specified child node, they are ordered lexicographically by key.

Objects come last and are sorted lexicographically by key in ascending order.

While your first query works fine, for your second query you could try the following code.

let aux = this.afData.list("/drivers", { query: { orderByChild: '/accountHistory/approved', startAt: false });

By doing this, your query results would not contain data with a value of null for your child node.

Though, I would recommend that you make sure that the data at the child node is of the same type for all the nodes to minimise the chances of Class cast exceptions and other errors. This is more of a hack.

Aaron Dunigan AtLee
  • 1,860
  • 7
  • 18
Rohan Stark
  • 2,346
  • 9
  • 16
  • Was there a breaking change within the last week? No matter what I do, limitToFirst or limitToLast, I cannot get an orderByChild('child-key') to return any items with no child-key. It used to be I could do an OrderByChild('child-key').equalTo(null) and it would return only values lacking that child. Now I get nothing. I haven't changed any code on my end. And I haven't updated any packages that I am aware of. – Luke Pighetti Aug 01 '18 at 11:41