如何在 Firebase 数据库中根据特定条件移除子节点或子节点?

huangapple go评论94阅读模式
英文:

How can I remove a child or childs which provide certain conditions in Firebase Database

问题

以下是翻译好的部分:

我对Firebase还不太熟悉,找不到从数据库中删除对象的方法。我已经搜索了大约一天的时间,当我尝试删除时,它会从数据库的taglist部分删除所有对象。这里是我的数据库结构以及我删除taglist对象 -M6QYGd-Hftv5NfS5KsI 的代码。然而,我找到的代码和我编写的代码会从数据库中删除所有taglist对象。是否有办法从数据库中删除名称为"AHA"的taglist对象。

英文:

I am new to firebase and I could not find a way to remove object(s) from my database. I have sarched for like one days and when I tried to delete it deletes all objects from my taglist part of database.Here are my database tree and my code to delete the taglist object -M6QYGd-Hftv5NfS5KsI. However the codes I found and I wrote deletes the all taglist objects from database. Is there a way to delete the taglist objects whose name is "AHA" from database

enter image description here

enter image description here

答案1

得分: 0

我看到你是新来的StackOverflow用户。发布代码的最佳方法是在问题框中键入代码,然后按下Ctrl+K,选择代码部分。

无论如何,谈到你的问题,当你调用以下代码时,你正在创建一个匿名(临时)查询:

databaseReferenceTagList.orderByChild("name").equalTo("AHA")

数据库中的引用(在数据库中具有物理存在)例如databaseReferenceTagList可以用查询来表示,因为DatabaseReference是继承自Query。由于它在数据库中具有物理存在,因此可以删除它。但并不是每个查询都需要引用(具有物理存在)。

你设计的查询包括对标签名称为"AHA"的引用(它们在数据库中具有物理存在)。然而,并不存在一个独立的物理存在,专门包含名称为"AHA"的标签列表。它只是更大的标签列表的一部分(当然在数据库中具有物理存在)。

因此,底线是:

  • 只能删除具有物理存在的项目
  • 具有物理存在的项目是具有引用的项目
  • 并非所有查询都需要引用,只有DatabaseReference对象才具有引用。

现在的一个大问题是,即使查询没有引用,它仍然具有数据和DataSnapshot。它的DataSnapshotgetRef()方法会返回查询最近父节点的引用,该父节点具有引用。

因此,当你调用临时查询的DataSnapshotgetRef()时,它返回的是标签列表的引用。

因此,在调用removeValue时会删除整个标签列表。

解决方法是只在实际引用的DataSnapshot上调用removeValue

在你的情况下,我们知道每个标签都实际存在,我们可以利用这一点来调用removeValue。因此,不要在dataSnapshot上调用removeValue,而是:

for (DataSnapshot d : dataSnapshot.getChildren()) {
    d.getRef().removeValue();
}

这将逐个删除查询的每个子项(在这种情况下仅有一个),而不删除引用(它是有问题的)本身。

如果有任何疑问,请随意发表评论。

英文:

I see you are new to stackOverflow. The best way to post your code is typing it in the question box itself and pressing ctrl+k by selecting the code part.

Anyway, Coming to your problem, you are creating an anonymous (temporary)query when you call

databaseReferenceTagList.orderByChild("name").equalTo("AHA")

A reference in the database(which has physical existence in the database) like databaseReferenceTagList can be represented by a query because DatabaseReference is inherited from Query. Since it has physical existence it can be deleted. But every query need not have a reference(physial existence).

The query you designed consists of the references to the tags whose name is "AHA"(they physically exist). However there is no exclusive physical existence for a list of tags whose name is "AHA". It is just a part of a bigger list-taglist(which of course has a physical existence on the database).

So the bottom line is

  • you can delete only those items which have a physical existence
  • The items having physical existence are those which have references
  • All queries need not have references. Only those which are DatabaseReference objects have a reference.

Now the big problem is even if a query does not have a reference, it has data and hence DataSnapShot. Its DataSnapshot's getRef() returns the reference of the nearest parent of the query which has a reference.

Therefore the (temporary)query you created returns the taglist reference when you call its datasnapshots' getRef.

And hence the whole taglist is removed on calling removeValue.

The solution is to call removeValue only on references you get from the datasnapshots of actual references.

In your case, we know that each tag has an actual existence and we can you that to call removeValue. Therefore instead of calling removeValue on dataSnapshot,

for(DataSnapshot d:dataSnapshot.getChildren()){
   d.getRef().removeValue();
}

This will individually delete each child(only one in this case) of the query and not the reference(which is faulty) itself.

Please feel free to comment if you have any doubts.

huangapple
  • 本文由 发表于 2020年5月4日 01:53:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/61579145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定