0
votes

I have a json data stored in firestore like below, the id of the doc is the date in ddmmyyyy format

29032019:{
   morning:[{
      person : 1,
      name:'abc',
      cancelled:false
     },
     {
      person : 2,
      name:'abc',
      cancelled:false
    }
  ],
  afternoon:[{
      person : 3,
      name:'abc',
      cancelled:false
   },
   {
      person : 4,
      name:'abc',
      cancelled:false
   }
 ]
}
30032019:{
   morning:[{
      person : 5,
      name:'abc',
      cancelled:false
     },
     {
      person : 2,
      name:'abc',
      cancelled:false
    }
  ],
  afternoon:[{
      person : 6,
      name:'abc',
      cancelled:false
   },
   {
      person : 7,
      name:'abc',
      cancelled:false
   }
 ]
}

here 9032019 and 30032019 are the document ids. How can i get the documents which contains a person inside the morning and afternoon arrays, so far i could do is to fetch documents by using next 3 upcoming dates and iterate through the morning and afternoon arrays to check a person with id 6 exists in the collection or not.

1
you can add the code you are using - diegoveloper

1 Answers

1
votes

With Firestore, you can't search for fields of objects inside arrays as you have now. You can only search arrays by the exact content of their items.

This means you will have to bring the values to search into a new field that is searchable. If you a searching by the person field, you could create a new array field in the document called people that contains only the integers of each person, then use an arrayContains query to find them.