0
votes

I am using Firebase Firestore database in my project. I have a data structure as below:

  • users (Collection)
    • user1 (document)
      • followingPeople (array)
        • 0
          • person0
        • 1
          • person1
        • 2
          • person2
        • 3
          • person3
    • user2 (document)
      • followingPeople (array)
        • 0
          • person0
        • 1
          • person1

I need to write a query which finds the users with a specific person in the following people part of the document fields.

For example if I query for the users which following person0 both user1 and user2 will be returned according to my example above.

Any suggestion will be appreciated.

1

1 Answers

1
votes

Storing the followingPeople in an array is an anti-pattern. Please read the Firestore documentation on working with arrays, lists and sets for a better approach.

followingPeople: {
    "person0": true,
    "person1": true,
    "person2": true
}

I'd also recommend reading my answer to http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value. While it was written for the Firebase Realtime Database, the same logic applies here.