我如何查询子集合firestore ionic 4 [英] How do I query subcollections firestore ionic 4

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

问题描述

你好,我想查询Firestore中的子集合,我具有以下结构

Hello I want to make a query to a sub-collection in firestore I have the following structure

"groups": {
        "g1":
        {
          "name": "Group 1",
          "users": {
              "u1": {
                          "id": "user1"
                    },
              "u2": {
                          "id": "user2"
                    }
             }
      },
     "g2":
        {
          "name": "Group 2",
          "users": {
              "u1": {
                          "id": "user1"
                    }
            }
       }

 }

"users": {
    "user1": {
                "firstName": "Lorem",
                "lastName": "Lorem"
             },

    "user2": {
                "firstName": "Lorem2",
                "lastName": "Lorem2"
             }

}

,我想查询一下,在示例中寻找user1会带给我属于该用户的组,这会给我带来g1和g2,但是如果我寻找user2的话我只会带给我g1,那么您可以在两者之间创建一个复合索引吗?组和用户?我正在用ionic 4开发它,我不知道数据是否结构合理

and I want to make a query that looking for user1 brings me the groups that belong that user in the example would bring me g1 and g2 but if I look for user2 should I only bring g1 can you create a composite index between the group and the user? I am developing it in ionic 4 I don't know if the data is well structured

非常感谢您能为我提供的帮助

Thank you very much in what you can help me

推荐答案

您可以使用 users 的"nofollow noreferrer> array_contains 操作.所需要做的就是知道数组应包含的完整,确切的元素.如果您知道所有这些,则可以使用以下方法进行检查:

You can use an array_contains operation on field users. All that requires is that you know the complete, exact element that the array should contains. If you know all of that, you can check with::

groupsRef.where("users", "array-contains", {
    u1": {
        "id": "user1"
    }
})

如果您只知道"user1" ,则需要一个仅包含"user1" 的数组.例如:

If you only know "user1", you will need to have an array that contains only "user1". So for example:

"userids": ["user1", "user2"]

然后您可以使用以下查询:

Then you can query with:

itiesRef.where("regions", "array-contains", "user1")

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

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