2

I am moving an Android app from rdbms to firebase and like a newbie in Firebase I am struggling with some queries.

I have the DB below in FB:

{
  "posts" : {
    "-KNnbTHJGm83zpSELWwB" : {
      ".priority" : "6gydj46kg0",
      "branch" : "Branch",
      "category" : "Categoria",
      "expiration" : "24h",
      "g" : "6gydj46kg0",
      "l" : [ -23.5415083, -46.88642 ],
      "photo" : "mPostPic_20160728192627.jpeg",
      "price" : 10,
      "priority" : 0,
      "product" : "ipad",
      "userKey" : "BNd8RP5qHoc0q7f9gn8cLjPy5Ux1",
      "offerMessage": "Valido ate as 14:00 hs",
      "likeCount": 0
    }
  }
}

in this post db I have a child "category". I have a list of some categories and I need to query only the posts where value category is inside this categories list.

I tried .start() and .end() but is not working.

2 Answers2

2

If your list of criteria is consecutive, you can get them with startAt()and endAt(). Since you say you already tried that, either you did something wrong (seeing your code would allow help in that case) or the criteria are not consecutive.

The latter case is more likely and means that your wish is a query equivalent to SQL's WHERE category IN ("Categoria", "Categorib", "Categoric"). Firebase doesn't have such an operation. Instead you'll have to do multiple queries with equalTo to accomplish the result.

Alternatively you can try to model the data differently to allow retrieving the posts for the criteria in one go. This isn't always possible, but if it is it follows the approach outlined in Query based on multiple where clauses in firebase

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    Yes Frank. This is exactly what I am looking for. I am trying to gain experience in Firebase but some old concepts are more complicate to implement. I will try to change data model and see if I can accomplish. I keep you informed. – Developer MobileCode Labs Aug 14 '16 at 15:15
0

You are searching for Firebase's function equalTo. You can do something like that:

Query myQuery = myFirebaseReference.child("posts")
    .orderByChild("category")
    .equalTo("Categoria");

Depending on your version of Firebase, myFirebaseReference can be a Firebase object or a DatabaseReference.

Sunshinator
  • 940
  • 11
  • 23