如何查询多个不同的子集合? [英] How can I query multiple different sub collection at ones?

查看:92
本文介绍了如何查询多个不同的子集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我正在构建一个食谱应用程序.我正在使用firebase作为我的数据库.现在,我想让用户找到我存储在我的食谱集中的所有食谱.

每个Food_Recipe具有不同的子集合.我将通过一个示例来说明我的问题.

现在,我想找到Recipe_name == Biriyani的文档. 我可以使用这种方式轻松归档目标,

  Query query = FirebaseFirestore.getInstance().collection("Recipes").document("Food_Recipe")
            .collection("Rice_Recipe").orderBy("Recipe_name")
            .startAt(InputSearchText).endAt(InputSearchText +"\uf8ff");

但是我需要在Recipe Collection中获得相同的结果. 例如,仅成像,如果Powder_Recipe也有一个Recipe_name Biriyani,则应该有2个来自不同子集的结果.

解决方案

如果要查询多个具有不同名称的(子)集合,则必须对每个集合使用专用查询并合并结果. /p>

如果要避免这种情况,则需要调整数据模型.

我知道将会有几个Food_Recipe文档,因为您写道每个Food_Recipe具有不同的子集合".

例如,在每个Food_Recipe文档下您只能有一个subRecipes子集合,并且在该子集合中,用文档中的字段来区分powderrice配方之间的区别(例如一个名为recipeType的字段).这样,您可以使用收藏组查询查询所有subRecipes子集合,如下所示:

Query query = FirebaseFirestore.getInstance().collectionGroup("subRecipes").orderBy("Recipe_name").startAt(InputSearchText).endAt(InputSearchText +"\uf8ff");

请注意,由于您已经具有Recipes根集合,因此我将子集合称为subRecipes.


如果要获取Food_Recipe文档的所有powder配方,则按如下所示过滤字段值:

Query query = FirebaseFirestore.getInstance().collection("Recipes").document("Food_Recipe").collection("subRecipes").whereEqualTo("recipeType", "powder");


更具体地说,拟议的数据模型如下

-- Recipes (root collection) 
   -- Food_Recipe_1 (document)
      -- subRecipes (subcollection)
         -- DocID#1 (document)
            - Recipe_Name: Biriyani (field)
            - recipeType: rice (field)             
            - ....  (field)     
         -- DocID#2 (document)
            - Recipe_Name: Peas Pulao (field)
            - recipeType: rice (field)             
            - .... (field)     
         -- DocID#3 (document)
            - Recipe_Name: Powder Recipe (field)
            - recipeType: powder (field)             
            - ....  (field)     
   -- Food_Recipe_2 (document)
      -- subRecipes (subcollection)
         -- DocID#4 (document)
            - Recipe_Name: Another Rice Recipe (field)
            - recipeType: rice (field)             
            - ....  (field)     
         -- DocID#5 (document)
            - Recipe_Name: Another Powder Recipe (field)
            - recipeType: powder (field)             
            - ....  (field)  

Recently I am building a recipe app. I am using firebase as my database. Now I want to let the users to find any recipe which I have stored in my recipe Collection.

Each Food_Recipe has different subcollections. I'll explain my issue using an example.

Now, I want to find the document which Recipe_name == Biriyani. I could archive my goal easily using this way,

  Query query = FirebaseFirestore.getInstance().collection("Recipes").document("Food_Recipe")
            .collection("Rice_Recipe").orderBy("Recipe_name")
            .startAt(InputSearchText).endAt(InputSearchText +"\uf8ff");

But I need to get the same result across the Recipe Collection. For example, Just imaging, if Powder_Recipe also have a Recipe_name Biriyani, there should be 2 result from different subcollections.

解决方案

If you want to query several (sub)collections with different names, you will have to use a dedicated query for each of those collections and merge the results.

If you want to avoid that you would need to adapt your data model.

I understand that there are going to be several Food_Recipe documents, since you wrote that "Each Food_Recipe has different subcollections".

So, for example, you could have only one subRecipes subcollection under each Food_Recipe document and, in this subcollection, make the difference between the powder and rice recipes with a field in the document (e.g. a field named recipeType). This way you can use a Collection Group query to query all the subRecipes subcollections, as follows:

Query query = FirebaseFirestore.getInstance().collectionGroup("subRecipes").orderBy("Recipe_name").startAt(InputSearchText).endAt(InputSearchText +"\uf8ff");

Note that I called the subcollections subRecipes, since you already have a Recipes root collection.


And if you want to get all the powder recipes for the Food_Recipe document, you filter on the field value, as follows:

Query query = FirebaseFirestore.getInstance().collection("Recipes").document("Food_Recipe").collection("subRecipes").whereEqualTo("recipeType", "powder");


More concretely, the proposed data model is the following

-- Recipes (root collection) 
   -- Food_Recipe_1 (document)
      -- subRecipes (subcollection)
         -- DocID#1 (document)
            - Recipe_Name: Biriyani (field)
            - recipeType: rice (field)             
            - ....  (field)     
         -- DocID#2 (document)
            - Recipe_Name: Peas Pulao (field)
            - recipeType: rice (field)             
            - .... (field)     
         -- DocID#3 (document)
            - Recipe_Name: Powder Recipe (field)
            - recipeType: powder (field)             
            - ....  (field)     
   -- Food_Recipe_2 (document)
      -- subRecipes (subcollection)
         -- DocID#4 (document)
            - Recipe_Name: Another Rice Recipe (field)
            - recipeType: rice (field)             
            - ....  (field)     
         -- DocID#5 (document)
            - Recipe_Name: Another Powder Recipe (field)
            - recipeType: powder (field)             
            - ....  (field)  

这篇关于如何查询多个不同的子集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆