Firebase中对象数组内的查找键 [英] Lookup key inside array of object in Firebase

查看:75
本文介绍了Firebase中对象数组内的查找键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我需要功能来查找联系信息数据,以检查用户是否已存在于我的数据库中.由于一个用户可能有多个电子邮件地址,因此我将这些数据存储在名为ContactInfo的单独的类中.

In my app I need the functionality to look up contact info data to check if a user already exists in my database. As it's possible that a user has multiple email addresses I'm storing this data in a separate class called ContactInfo.

现在,我要查询所有对象,并检查ContactInfo内的 emails数组中是否包含电子邮件.因此,我将电子邮件用作密钥,这就是为什么它是URL编码的原因.但是,我找不到能实现此目标的正确查询.

Now I want to query all objects and check if an email is contained in the emails array inside ContactInfo. Therefore I'm using the email as key, that's why it's URL encoded. However I can't figure out the right query to achieve this.

contactInfo

  • -KYd3pbZ7D--yX6B3HY8
    • 电子邮件
      • john%40gmail%2Ecom:是
      • john%40hotmail%2Ecom:是
      • -KYd3pbZ7D--yX6B3HY8
        • emails
          • john%40gmail%2Ecom: true
          • john%40hotmail%2Ecom: true
          • 电子邮件
            • jane%40gmail%2Ecom:是
            • emails
              • jane%40gmail%2Ecom: true

              基于这一点,我数据库的顶层看起来像这样

              Based on that the top level of my database looks like this

              推荐答案

              使用电子邮件地址作为密钥可能不是您想要的.

              Using an email address as a key is probably not what you want to do.

              电子邮件地址发生更改并被视为动态数据-最佳做法是,不要将动态数据(可能会更改的内容)用作键,就好像它确实在更改一样,在数据库中引用的所有位置也都必须进行更新.

              email addresses change and are considered dynamic data - it's best practice not use dynamic data (something that may change) as a key as if it does change, everywhere it's referenced in your database would also have to be updated.

              一种可能性是像这样存储多封电子邮件:

              One possibility is to store multiple emails as such:

              contactInfo
                  -KYd3pbZ7D--yX6B3HY8
                       firstname: "John"
                       lastname: "Doe"
                       main_email: "-JYJkjajisaiisd"
              

              ,然后是所有电子邮件的单独节点.

              and then a separate node for all of the emails.

              contact_emails
                  -JYJkjajisaiisd
                     email : "john@gmail.com"
                     uid : "uid_0"
                  -YJNlkokaosomdo
                     email : "john@hotmail.com",
                     uid : "uid_0"
                  -Juiaisidiasda
                     email : "frank@fmail.com",
                     uid : "uid_3"
              

              此结构是可查询的,可维护的,并且避免了担心将特殊字符(电子邮件字符)作为键进行解析/存储的麻烦.您可以每人存储多封电子邮件,也可以通过更新他们的contactInfo中的main_email节点来更改其主要电子邮件地址

              This structure is query-able, maintainable and avoids having to worry about parsing/storing special characters (email chars) as a key. You can store multiple emails per person and you can change their main email by just updating the main_email node within their contactInfo

              要检查电子邮件,请使用此查询

              To check for emails, use this query

                  contactEmailsRef.queryOrdered(byChild:"email")
                        .queryEqual(toValue: "john@hotmail.com")
                        .observeSingleEvent(of: .value, with: { snapshot in
              
                              print(snapshot)
              
                  })
              

              将导致该节点被打印

              -YJNlkokaosomdo
                   email : "john@hotmail.com",
                   uid : "uid_0"
              

              这篇关于Firebase中对象数组内的查找键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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