【问题标题】:How to structure a recipe database in firebase? [duplicate]如何在 Firebase 中构建配方数据库? [复制]
【发布时间】:2018-12-06 06:16:14
【问题描述】:

我正在制作一个应用程序来获取成分,而不是显示可以用这些成分制成的食谱。食谱将存储在 fire-base 中。 我正在考虑使用的结构如下所示:

{
"description" : "Some description",
"name" : "Recipe name",
"idRecipe" : 1,
"ingredients" : {
                 "ingredient1" : "tomato",
                 "ingredient2" : "pepper",
                 .
                 .
                 .
                 "ingredient10": "cheese",
                },
"numOfPersons" : 2,

} 

我如何'查询'具有匹配成分的食谱,或者应该改变食谱的结构?

【问题讨论】:

  • 在您当前的数据模型中,您可以轻松找到给定食谱的成分。但是,您不能有效地查询给定成分的食谱。您需要为此创建一个额外的数据结构。请在此处查看我的解释以获取更多信息:stackoverflow.com/questions/40656589/…
  • 或者,看看 Cloud Firestore,它更本机地处理此类分类查询,只要您将它们存储在数组中:firebase.googleblog.com/2018/08/…

标签: database firebase firebase-realtime-database


【解决方案1】:

你可以把你的模型改成这样:

{
"description" : "Some description",
"name" : "Recipe name",
"idRecipe" : 1,
"ingredients" : {
                 "tomato" : true,
                 "pepper" : true,
                 .
                 .
                 .
                 "cheese": true
                },
"numOfPersons" : 2,

}

然后你可以使用多个wherequeries来获取这个配方,比如:

query = recipeRef.where("ingredients.tomato", "==", true).where("ingredients.pepper", "==", true).where("ingredients.cheese", "==", true);

【讨论】:

  • 您的查询语法适用于 Cloud Firestore,但 OP 使用的是实时数据库。使用当前(或您建议的)数据结构无法有效地进行此类查询,因为它们要求您为每种成分定义一个索引。更多信息请参见我的解释:stackoverflow.com/questions/40656589/…
猜你喜欢
  • 1970-01-01
  • 2011-05-01
  • 2020-01-20
  • 2017-10-19
  • 1970-01-01
  • 2018-01-02
  • 1970-01-01
  • 2021-07-12
相关资源
最近更新 更多