[英]I want to delete value from Firebase Realtime Databse by value i don't have it's key
I want to delete value from firebase realtime database by value I don't have it's key我想按值从 firebase 实时数据库中删除值我没有它的关键
I have database like:我有这样的数据库:
-root
|
--fittingHeight
|
--"timeStamp1":"1.1"
|
--"timeStamp2":"2.1"
I want to delete 1.1 in this database but without key (timestamp1)我想删除此数据库中的 1.1 但没有密钥(时间戳 1)
You need to query the collection and then order it by its key您需要查询集合,然后按其键对其进行排序
Query dbQuery = myRef.orderByKey();
Then you will need to add it a listener and use for loop on the results然后你需要给它添加一个监听器并在结果上使用 for 循环
dbQuery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot dst : dataSnapshot.getChildren()){
if(...)
myRef.child(dst.getKey()).removeValue();
}
}
}
This will remove all the values in that key so if you want to remove a specific value inside that key, add another for loop on its results.这将删除该键中的所有值,因此如果您想删除该键中的特定值,请在其结果上添加另一个 for 循环。
I want to delete 1.1 in this database but without key (timestamp1)我想删除此数据库中的 1.1 但没有密钥(时间戳 1)
There is no way you can use a query based on a property field which is dynamic.您无法使用基于动态属性字段的查询。 This is happening because, in order to delete a value, you need to know it's key.发生这种情况是因为,为了删除一个值,您需要知道它的键。
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("fittingHeight").orderByChild("timeStamp1").equalTo("1.1");
In Firebase Realtime Database there is no concept of wildcards.在 Firebase 实时数据库中没有通配符的概念。 So you always need to specify the name of the property.所以你总是需要指定属性的名称。 It's true that you can get the content of the entire fittingHeight
object and do the filtering on the client but this solution isn't practical at all.确实可以得到整个fittingHeight
object 的内容并在客户端进行过滤,但这种解决方案根本不实用。 A solution might be to change the database schema to have fixed properties, in a way that the above query can work.一种解决方案可能是将数据库模式更改为具有固定属性,以使上述查询可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.