2

I'm thinking about migrating to Cloud Firestore from realtime ratabase and wondering how I can properly setup the datastructure for my localized content.

This is how it was structured in RTDB:

articles
   en-US
      article_01
   de-DE
      article_01

In Firestore, would I create something like this?

Collection: articles
   document: article_01

And then nest all the data of article_01 in two maps called 'en-US' and 'de-DE'?

Not sure if there's not a better way to structure localized data in Firestore?

Thanks for any help :)

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
phk
  • 23
  • 4

1 Answers1

4

There is a simple way for structuring such data in Cloud Firestore. So a possible schema might be:

Firestore-root
   |
   --- articles (collection)
        |
        --- articleId (document)
        |     |
        |     --- language: "en-US"
        |     |
        |     --- //other article properties
        |
        --- articleId (document)
              |
              --- language: "de-DE"
              |
              --- //other article properties

Using this database schema, in Android, you can simply:

  • Get all articles regardless of the language using just a CollectionReference:

    FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
    CollectionReference articlesRef = rootRef.collection("articles");
    articlesRef.get().addOnCompleteListener(/* ... */);
    
  • Get all articles that correpond to a single language using a Query:

    FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
    Query query = rootRef.collection("articles").whereEqualTo("language", "en-US");
    query.get().addOnCompleteListener(/* ... */);
    
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193