0

I want to get all the posts that have the key "type" equal to 1 or 2 this code doesn't work

@Override
public Query getQuery(DatabaseReference databaseReference) {
    return databaseReference.child("posts").orderByChild("type").equalTo(1).orderByChild("type").equalTo(2);  
}`

but this one works for only type 1:

@Override
public Query getQuery(DatabaseReference databaseReference) {
    return databaseReference.child("posts").orderByChild("type").equalTo(1);   
}

How can I have posts that contains both values for the key "type"

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Lotfi
  • 660
  • 1
  • 6
  • 21

1 Answers1

1

You can use a range filter (Edited):

return databaseReference.child("posts")
                        .orderByChild("type")
                        .startAt(1)
                        .endAt(2)

But this will only work for filtering on a consecutive range (like here). It won't work if you want items with type 1 or 3, but not with 2. Firebase doesn't support such multi-operand queries.

Also see:

Lotfi
  • 660
  • 1
  • 6
  • 21
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807