2

I'm using Firestore, and I don't see the benefit in using a subcollection versus using a 'foreign' key. When I get a document, the subcollection isn't loaded with that document.

What would be the benefit of using a subcollection, for example

books: {
    1: {
        "name": "Book 1"
     }
}
comments: {
    "bookId": 1,
    "content": "good book"
}

Or using a subcollection

books: {
    name: "Book 1",
    comments: [{...}]
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Miguel Stevens
  • 8,631
  • 18
  • 66
  • 125

2 Answers2

3

The advantage to using the sub-collections is that you gather information about the collection in itself, and you must use this approach if you do not need to retrieve all the sub collections at once. for example: a user and his articles of blog: if you must be able to retrieve the articles of all the users you use the relations with the foreign key but for the comments you use the sub-collections for a better organization of your database.

wamba kevin
  • 308
  • 3
  • 9
1

When I get a document, the subcollection isn't loaded with that document.

This is because the queries in Firestore are shallow: they only get items from the collection that the query is run against. There is no way to get documents from a top-level collection and a subcollections in a single query. Firestore doesn't support queries across different collections in one go.

What would be the benefit of using a subcollection

The benefit is that you can create an unlimited number of documents. In case of storing data in a document, keep in mind the documents have limits. So there are some limits when it comes to how much data you can put into a document. According to the official documentation regarding usage and limits:

Maximum size for a document: 1 MiB (1,048,576 bytes)

As you can see, you are limited to 1 MiB total of data in a single document. When we are talking about storing text, you can store pretty much but if you decide to store complex objects, be careful about this limitation.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I don't understand the benefit of creating an unlimited number of documents with a subcollection, this can also be done with a topleven collection right? So it's the same thing? – Miguel Stevens Nov 30 '18 at 02:21
  • Yes, you can do it also in a top level collection. What I was trying to tell is that in a collection, you can add as many documents you want while in a document you can add limited data, right? – Alex Mamo Nov 30 '18 at 10:57
  • Btw, "you do not need to retrieve all the sub collections at once" There is no way in Firestore to do that. Firestore doesn't support queries across different collections or subcollections. "if you must be able to retrieve the articles of all the users you use the relations with the foreign key but for the comments you use the sub-collections " That's totally wrong. You can do this either in a collection or in a subcollection. This are not the benefits of using a subcollection at all. I don't recommend future visitors that approach. – Alex Mamo Nov 30 '18 at 11:19
  • See **[here](https://stackoverflow.com/questions/52469492/efficiency-of-searching-using-wherearraycontains)** more informations about using arrays, maps or collections/subcollections. Hope you'll better understand the differences. And hope you reconsider the answer. – Alex Mamo Nov 30 '18 at 11:21