1

I am creating a social app using firestore like Instagram or Twitter where user can see posts of the users they follow on their wall.

Firestore whereArrayContains|whereArrayContainsAny only works for 10 items, whereas a user can follow more than 10 people. So I decided to use map filter as done here.

postCollection
        - postID_1
                - First Post By Person 1
                - createdBy 
                     person_1_ID = true 
        - postID_2
                - Second Post By Person 1
                - createdBy 
                     person_1_ID = true 
        - postID_3
                - Third Post By Person 1
                - createdBy 
                     person_1_ID = true 
        - postID_4
                - First Post By Person 2
                - createdBy 
                     person_2_ID = true 

Code

    val fs = Firebase.firestore
    var query  = fs.collection("postCollection").whereEqualTo("createdBy.person_1_ID",true)
    query = query.whereEqualTo("createdBy.person_2_ID",true)
    query.get().addOnSuccessListener {
        val mList = it.toObjects(PostBO::class.java)
    }

Above code working for single

whereEqualTo("createdBy.person_1_ID",true)

I have seen multiple post on stackoverflow like this and this

Or suggest a better structure for handling.

Show recent post for those to whom I follow.

2nd Approach

var query  = fs.collection("postCollection")
query = query.whereEqualTo("createdBy", hashMapOf(
    ///"person_2_ID" to true,
    "person_1_ID" to true
))
query = query.whereEqualTo("createdBy", hashMapOf(
    "person_2_ID" to true
))
query.get().addOnSuccessListener {
    val mList = it.toObjects(PostBO::class.java)
    mList.forEach { _ ->

    }
}

Have a look at my Document below enter image description here

Still not working for multiple WhereEqual.

Edit after AlexMamo Answer

You solution doesn't work. Just for testing purpose I added a node to test and operation like below still give me no data at all.

postCollection
        - postID_1
                - First Post By Person 1 and Person 2 both
                - createdBy 
                     person_1_ID = true 
                     person_2_ID = true

Your answer also don't work for AND operation as well. But I need OR operation

Query working for And Operation is written below.

val query  = fs.collection("testCollection")
            .whereEqualTo("createdBy.person_1_ID", true)
            .whereEqualTo("createdBy.person_2_ID",true)
        query.get().addOnSuccessListener {
            val mList = it.toObjects(PostBO::class.java)
            mList.forEach { _ ->
                //
            }
        }
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300

0 Answers0