在Firebase中查询父节点的所有实例中的特定子节点 [英] Query a specific child node in all instances of a parent node in Firebase

查看:180
本文介绍了在Firebase中查询父节点的所有实例中的特定子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在Firebase中的所有父节点实例中查询特定的子节点。可以假设所有查询的父节点都有这个特定的子节点。



在这个例子中, uid 是每个用户的唯一标识符,我试图得到 displayNames 列表:

  {
users:{
uid:{
displayName:stringOfSomeKind
}
uid2:{
displayName: moreStrings
}
uid3:{
displayName:evenMoreStrings
}
...
}
}

这样做的目的是让我可以检查 displayName 目前正在采取。 (我不能使用 displayName 作为主键,因为当用户登录时,我只会有 uid displayNames 中的一个是否已经被使用了?

我是否必须对数据进行非规范化才能有效地执行此操作?如果是这样的话,怎么样?当你从Firebase加载一个节点的时候,你也可以得到该节点下的所有数据。假设每个用户拥有的数据不仅仅是他们的显示名称,这确实会导致不必要的数据加载。

如果您只想加载一个显示名称列表,您应该确实存储显示名称列表。

  {
displayNames:{
stringOfSomeKind: uid,
moreStrings:uid2,
evenMoreStrings:uid3
}
}
pre>

如果您来自关系数据库/ SQL数据库的背景,那么起初可能看起来不自然。对我来说,它有助于认识到这些结构是索引,与您可能添加到关系数据库的display by displayName相同。不同的是,在NoSQL / Firebase中,它是维护索引的代码,而在大多数RDBMS中,这样的索引由系统维护。


I'm trying to figure out how, if possible, to query a specifically named child node in all instances of a parent node in Firebase. It can be assumed that all parent nodes queried have this specifically named child node in it.

In this example, uid is a unique identifier for each user and I'm trying to get a list of displayNames:

{
  users: {
    uid: {
      displayName: "stringOfSomeKind"
    }
    uid2: {
      displayName: "moreStrings"
    }
    uid3: {
      displayName: "evenMoreStrings"
    }
    ...
  }
}

The purpose of this is so I can check to see if a displayName is currently taken. (I can't use the displayName as the primary key because when a user logs in, I'll only have the uid available.)

How can I efficiently check to see if one of these displayNames is already taken? Do I have to denormalize my data to do so efficiently? If so, how?

解决方案

When you load a node from Firebase, you also get all data under that node. Assuming that you have more data per user than just their display name, that can indeed lead to needlessly loaded data.

If you only want to load a list of display names, you should indeed store a list of display names.

{
  displayNames: {
    "stringOfSomeKind": "uid",
    "moreStrings": "uid2",
    "evenMoreStrings": "uid3"
  }
}

If you come from a background of relational/SQL databases, this may seem unnatural at first. For me it helped to realize that these structures are indexes, same as the "index by displayName" that you might add to your relational database. The difference is that in NoSQL/Firebase it is your code that maintains the index, while in most RDBMSs such indexes are maintained by the system.

这篇关于在Firebase中查询父节点的所有实例中的特定子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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