【问题标题】:How to properly structure localized content in Cloud Firestore?如何在 Cloud Firestore 中正确构建本地化内容?
【发布时间】:2019-01-08 11:19:03
【问题描述】:

我正在考虑从实时 Ratabase 迁移到 Cloud Firestore,并想知道如何为我的本地化内容正确设置数据结构。

这是它在 RTDB 中的结构:

articles
   en-US
      article_01
   de-DE
      article_01

在 Firestore 中,我会创建这样的东西吗?

Collection: articles
   document: article_01

然后将article_01的所有数据嵌套在'en-US'和'de-DE'两个map中?

不确定是否有更好的方法在 Firestore 中构建本地化数据?

感谢您的帮助:)

【问题讨论】:

    标签: java android firebase google-cloud-firestore


    【解决方案1】:

    有一种在 Cloud Firestore 中构建此类数据的简单方法。所以一个可能的模式可能是:

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

    使用这个数据库架构,在 Android 中,您可以简单地:

    • 使用CollectionReference获取所有文章,无论语言如何:

      FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
      CollectionReference articlesRef = rootRef.collection("articles");
      articlesRef.get().addOnCompleteListener(/* ... */);
      
    • 使用Query 获取与单一语言相对应的所有文章:

      FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
      Query query = rootRef.collection("articles").whereEqualTo("language", "en-US");
      query.get().addOnCompleteListener(/* ... */);
      

    【讨论】:

    • 太棒了,谢谢亚历克斯!这意味着我将所有文章添加到同一个集合中,而不管它们的语言如何,然后只查询那些具有我需要的属性语言的文章,对吗?
    • @phk 是的,没错。
    • 有了这个解决方案,你怎么知道两者是同一篇文章,只是语言不同?
    • 现在我们可以使用 Firestore 数组。
    • 您能否将您的示例更新为带有数组的版本?谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    相关资源
    最近更新 更多