0

I have a list of products, e.g.

Name
ImageURL
Type
Description

Imagine I wanted to do the following query in an iOS app:

  • Determine how many unique types of product are in the list and present 5 items at random from each type.

E.g.

Type A
  - 0
  - 1
  - 2
  - 3
  - 4
Type B
  - 5
  - 6
  - 7
  - 8 
  - 9

What would be the best way to set up a Firebase Cloud Firestore db to support this?

I was thinking to pre-process the list so do the following

Collection 
  - Document (TypeA)
    - Collection (Product1)
    - Collection (Product2)
    - Collection (Product3)  
    - Collection (Product4)  
    - Collection (Product5)  
  - Document (TypeB)
    - Collection (Product6)
    - Collection (Product7)
    - Collection (Product8)  
    - Collection (Product8)  
    - Collection (Product9)  

but then I'm not sure how the query would work?

E.g.

 db.collection("Products").getDocuments().getCollections().limit(to: 5)
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
user11145365
  • 184
  • 9
  • I'm not sure I understand the problem. As in: what isn't working in your current approach? If you're wondering how to get random documents from a collection, see https://stackoverflow.com/questions/46798981/firestore-how-to-get-random-documents-in-a-collection – Frank van Puffelen Mar 03 '19 at 22:47
  • Thanks @frankvanpuffelen - the problem is its not possible to extract a list of subcollections with a limit (the example query doesn't work). – user11145365 Mar 04 '19 at 08:59
  • If you're asking whether it is possible to query across multiple (sub)collections, then that is indeed not possible. See https://stackoverflow.com/questions/46573014/firestore-query-subcollections – Frank van Puffelen Mar 04 '19 at 15:05
  • @FrankvanPuffelen thanks, it seems like its rather pointless to use Firestore in that case? – user11145365 Mar 04 '19 at 19:06
  • That's a rather broad statement. I have many great reasons to use Firestore, but whether those apply to you is impossible for me to say. – Frank van Puffelen Mar 05 '19 at 03:47

0 Answers0