Firebase complex "contain" queries

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.

Upvotes: 2

Views: 2653

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 598797

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

Upvotes: 2

Sunshinator
Sunshinator

Reputation: 950

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.

Upvotes: 0

Related Questions