Google firebase检查是否存在子级

时间:2022-11-06 15:23:47

In my app, I need to check if a given element of my database on firebase has a child with a given name. I hoped it could be done by using something along the lines of:

在我的应用程序中,我需要检查firebase上我的数据库的给定元素是否具有给定名称的子项。我希望可以通过以下方式使用以下内容来完成:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();

if (rootRef.childExists("name")) {
    //run some code
}

I searched but I couldn't find anything useful.

我搜索过,但找不到任何有用的东西。

2 个解决方案

#1


52  

Edit 2; worth putting on top: I think it is worth mentioning that this is actually downloading all data at this snapshot just to check whether any data exists. You should be mindful here. If the reference is huge (e.g. actually the root reference and not a specific child/property) then you should either find a deeper node you can use to check for existence or design your data structure differently so an efficient check is possible.

编辑2;值得一提的是:我认为值得一提的是,这实际上是在这个快照下载所有数据,只是为了检查是否存在任何数据。你应该留意这里。如果引用很大(例如实际上是根引用而不是特定的子/属性),那么您应该找到可用于检查存在的更深层节点,或者以不同方式设计数据结构,以便进行有效检查。

A database reference is effectively the URL for that data. You want to actually get data to see whether a child exists. This is why the method you seem to be looking for is on DataSnapshot.

数据库引用实际上是该数据的URL。您希望实际获取数据以查看子项是否存在。这就是您似乎正在寻找的方法是DataSnapshot的原因。

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
  @Override
  void onDataChange(DataSnapshot snapshot) {
    if (snapshot.hasChild("name")) {
      // run some code
    }
  }
});

Now, this design pattern feels a bit strange. You're reading the whole database just to see whether "name" exists. You can make this a bit more efficient by listening to rootRef.child("name") and then just checking whether snapshot.exists().

现在,这种设计模式感觉有点奇怪。您正在阅读整个数据库,只是为了查看“名称”是否存在。您可以通过监听rootRef.child(“name”)然后只检查snapshot.exists()来提高效率。

If you're trying to do validation here, and not control flow, you should consider putting this code in your rules.json.

如果您尝试在此处进行验证,而不是控制流程,则应考虑将此代码放在rules.json中。

edit: I originally used the wrong function name (childExists instead of hasChild)

编辑:我最初使用了错误的函数名称(childExists而不是hasChild)

#2


2  

Try using .childexists in combination with .equalTo("Your specific name")

尝试将.childexists与.equalTo(“您的具体名称”)结合使用

#1


52  

Edit 2; worth putting on top: I think it is worth mentioning that this is actually downloading all data at this snapshot just to check whether any data exists. You should be mindful here. If the reference is huge (e.g. actually the root reference and not a specific child/property) then you should either find a deeper node you can use to check for existence or design your data structure differently so an efficient check is possible.

编辑2;值得一提的是:我认为值得一提的是,这实际上是在这个快照下载所有数据,只是为了检查是否存在任何数据。你应该留意这里。如果引用很大(例如实际上是根引用而不是特定的子/属性),那么您应该找到可用于检查存在的更深层节点,或者以不同方式设计数据结构,以便进行有效检查。

A database reference is effectively the URL for that data. You want to actually get data to see whether a child exists. This is why the method you seem to be looking for is on DataSnapshot.

数据库引用实际上是该数据的URL。您希望实际获取数据以查看子项是否存在。这就是您似乎正在寻找的方法是DataSnapshot的原因。

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
  @Override
  void onDataChange(DataSnapshot snapshot) {
    if (snapshot.hasChild("name")) {
      // run some code
    }
  }
});

Now, this design pattern feels a bit strange. You're reading the whole database just to see whether "name" exists. You can make this a bit more efficient by listening to rootRef.child("name") and then just checking whether snapshot.exists().

现在,这种设计模式感觉有点奇怪。您正在阅读整个数据库,只是为了查看“名称”是否存在。您可以通过监听rootRef.child(“name”)然后只检查snapshot.exists()来提高效率。

If you're trying to do validation here, and not control flow, you should consider putting this code in your rules.json.

如果您尝试在此处进行验证,而不是控制流程,则应考虑将此代码放在rules.json中。

edit: I originally used the wrong function name (childExists instead of hasChild)

编辑:我最初使用了错误的函数名称(childExists而不是hasChild)

#2


2  

Try using .childexists in combination with .equalTo("Your specific name")

尝试将.childexists与.equalTo(“您的具体名称”)结合使用