0

I’m trying firebase database and one question comes up.

Before posting, I read this, this, and this github but not figure how to do this on Android (if it's a possible to do this on js, why not Java?)

I have a note with a GUID on the key, and I have two timestamps properties, startTime and endTime. I get the actual timestamp from user, and I need to retrieve all notes where the user timestamp is between startTime and endTime.

Here is an entity sample on firebase:

notes:

{
  "notes": {
    "aedbd23a-ca8e-4160-88a0-1d68f92b34cf": {
      "description": "Note test",
      "startTime": 1490353200,
      "endTime": 1490396400
    }
  }
}

On plain SQL I can achieve this doing select * from notes where userTime between startTime and endTime. How can I achieve this on firebase?

Community
  • 1
  • 1

2 Answers2

1

You can use Query link

endAt(String value) : Create a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default.

startAt(String value) : Create a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default.

Oussema Aroua
  • 5,225
  • 1
  • 24
  • 44
  • I'm already using these methods, but the point here is how can I query between two properties. If I order by endTime for example, I can't order using anoter key and filter it (throws an exception). And Query needs a value to filter. Trying to illustrate better, the query in plain SQL is (1490353200 >= startTime) and (1490353200 <= endTime) – Grupo CDS Informática Mar 27 '17 at 19:31
0
{
"notes": {
  "aedbd23a-ca8e-4160-88a0-1d68f92b34cf": {
    "description": "Note test",
    "startTime": 1490353200,
    "endTime": 1490396400,
    "startTime_endTime": 1490353200_1490396400   
    }
  }
}

Query will be

ref.child("notes")
.orderByChild("startTime_endTime")
.startAt("1490353200_1490353200")
.endAt("1490353250_1490353250")

In firebase use the composite key when you need more filters.
Try this one and tell me if it works or no. In start at give small value and endat give bigger value.