我正在考虑从实时数据库迁移到 Cloud Firestore,并想知道如何为本地化内容正确设置数据结构。
这就是 RTDB 中的结构:
articles
en-US
article_01
de-DE
article_01
在 Firestore 中,我会创建这样的东西吗?
Collection: articles
document: article_01
然后将article_01
的所有数据嵌套在名为“en-US”和“de-DE”的两个映射中?
不确定是否有更好的方法在 Firestore 中构建本地化数据?
感谢您的帮助:)
最佳答案
有一种简单的方法可以在 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(/* ... */);
使用
查询
获取与单一语言对应的所有文章:FirebaseFirestore rootRef = FirebaseFirestore.getInstance(); Query query = rootRef.collection("articles").whereEqualTo("language", "en-US"); query.get().addOnCompleteListener(/* ... */);
关于java - 如何在 Cloud Firestore 中正确构建本地化内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54090699/