Database 如何在firebase中构造配方数据库?
我正在制作一个应用程序,它可以获取配料,然后显示可以从这些配料中制作的配方。这些配方将存储在fire base中。 我想采用的结构如下所示:Database 如何在firebase中构造配方数据库?,database,firebase,firebase-realtime-database,Database,Firebase,Firebase Realtime Database,我正在制作一个应用程序,它可以获取配料,然后显示可以从这些配料中制作的配方。这些配方将存储在fire base中。 我想采用的结构如下所示: { "description" : "Some description", "name" : "Recipe name", "idRecipe" : 1, "ingredients" : { "ingredient1" : "tomato", "ingredient2" : "pepper
{
"description" : "Some description",
"name" : "Recipe name",
"idRecipe" : 1,
"ingredients" : {
"ingredient1" : "tomato",
"ingredient2" : "pepper",
.
.
.
"ingredient10": "cheese",
},
"numOfPersons" : 2,
}
{
"description" : "Some description",
"name" : "Recipe name",
"idRecipe" : 1,
"ingredients" : {
"tomato" : true,
"pepper" : true,
.
.
.
"cheese": true
},
"numOfPersons" : 2,
}
我如何“查询”含有匹配成分的配方,或者应该更改配方的结构?您可以将模型更改为如下:
{
"description" : "Some description",
"name" : "Recipe name",
"idRecipe" : 1,
"ingredients" : {
"ingredient1" : "tomato",
"ingredient2" : "pepper",
.
.
.
"ingredient10": "cheese",
},
"numOfPersons" : 2,
}
{
"description" : "Some description",
"name" : "Recipe name",
"idRecipe" : 1,
"ingredients" : {
"tomato" : true,
"pepper" : true,
.
.
.
"cheese": true
},
"numOfPersons" : 2,
}
然后,您可以使用多个where
来获取此配方,如:
query = recipeRef.where("ingredients.tomato", "==", true).where("ingredients.pepper", "==", true).where("ingredients.cheese", "==", true);
您的查询语法适用于Cloud Firestore,但OP使用的是实时数据库。对于当前(或您提议的)数据结构,这样的查询是不可能有效的,因为它们要求您为每个成分定义一个索引。更多信息,请参见我的解释:在当前的数据模型中,您可以轻松找到给定配方的成分。然而,您无法高效地查询给定配料的配方。您需要为此创建一个额外的数据结构。更多信息,请参见我的解释:或者,看看Cloud Firestore,它可以更自然地处理此类分类查询,只要将它们存储在数组中: