How to properly structure localized content in Cloud Firestore?

北城余情 提交于 2020-06-17 02:59:25

问题


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 :)


回答1:


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(/* ... */);
    


来源:https://stackoverflow.com/questions/54090699/how-to-properly-structure-localized-content-in-cloud-firestore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!