0
votes

I'm trying to migrate the following structure from real time database to Firestore :

•   Resources
    o   SENT
           resId1
               •    name : xxxx
               •    url : xxx
           resI2
               •    name ……
   o    ACCEPT
           resId3
           etc……
   o    REFUSED
           restIdn 
           etc….

So under root Node" Resources" I have some sub nodes (SENT, ACCEPT, REFUSED, ...) that contain List of resources items.

With Firestore it seems I can't have subCollection directly under collection (When I try to reproduce this structure with Firestore in the admin console I need to create an intermediate document like:

Collection --> document --> SubCollection --> documents

Witch lead to that structure:

Resources --> SENT --> SENT --> resId1 {name: xxx, url: yyyy}

So the sub node "SENT" is duplicated twice (one for the document and one for the sub collection). This is not an improvement or a simplification at all, if I compare with firebase real time database.
Do I miss something? What is the best way for such kind of database structure?

1

1 Answers

3
votes

(Edited 10/13/2017 @ 11am)

It seems that there are two structures that will work for you.

Option 1: Three Root Collections

Create three collections at the root of your Firestore database

  • resources-sent
  • resources-accept
  • resources-refused

Each one contains documents.

Option 2: One Root Collection

Firestore allows for compound queries, so you could just make one collection at the root called resources and add a type parameter to each document where type is one of [sent, accept, refused].

Then you can do queries like this:

// Get all sent resources
db.collection("resources").where("type", "==", "sent").get()

Because of Firestore's built in indexes this query will always be fast!'

Option 3: Subcollections.

Create a root collection called resources containing only three docs:

  • sent
  • accept
  • refused

For each of these docs create a resources subcollection.

So to get all sent resources:

// Get all sent resources
db.collection("resources").doc("sent").collection("resources").get()