简体   繁体   English

如何检查 firebase 数据库中是否存在唯一条目?

[英]How to check if unique entry exists in firebase database or not?

Basically, I want to check if a unique key already exists in the firebase database.基本上,我想检查 firebase 数据库中是否已经存在唯一键。

在此处输入图像描述

I have tried using this:我试过使用这个:

Query query = myRef.child("Device-1").orderByChild(Objects.requireNonNull(myRef.child("Device-1").getKey())).equalTo(null);
query.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if(dataSnapshot.exists()) {
            Toast.makeText(getApplicationContext(), "ye", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
});

But that just doesn't work and I suspect there is another method to do it.但这行不通,我怀疑还有另一种方法可以做到这一点。

To check if a particular node exists in the Realtime Database, then you have to create a reference that points exactly to that node.要检查实时数据库中是否存在特定节点,您必须创建一个精确指向该节点的引用。 So in code, it should look like this:所以在代码中,它应该是这样的:

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference devicesRef = db.child("Devices");
DatabaseReference deviceOneRef = devicesRef.child("Device-1");
DatabaseReference keyRef = deviceOneRef.child("-NJLkBkt96yj6gg0EC1n");
keyRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            DataSnapshot snapshot = task.getResult();
            if(snapshot.exists()) {
                Log.d("TAG", "The document exists.");
            } else {
                Log.d("TAG", "The Document doesn't exist.");
            }
        } else {
            Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
        }
    }
});

If you don't know the key ("-NJLkBkt96yj6gg0EC1n"), then you have to perform a query that uniquely identifies that node, for example the "deviceid".如果您不知道密钥(“-NJLkBkt96yj6gg0EC1n”),则必须执行唯一标识该节点的查询,例如“deviceid”。 In code, that would be:在代码中,这将是:

Query queryByDeviceId = devicesRef
        .orderByChild("deviceid")
        .equalTo("BEGIN:VCARD VERSION:3.0 N:;DEVICE-8569 END:VCARD");
queryByDeviceId.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            for (DataSnapshot ds : task.getResult().getChildren()) {
                if(ds.exists()) {
                    Log.d("TAG", "The document exists.");
                } else {
                    Log.d("TAG", "The Document doesn't exist.");
                }
            }
        } else {
            Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
        }
    }
});

If you want to update multiple record when you perfrom a query, then please check my answer from the following post:如果您想在执行查询时更新多条记录,请查看我在以下帖子中的回答:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查 Firebase 实时数据库中是否存在条目(Python) - Check if entry exists in Firebase Realtime Databse (Python) 如何在 Firebase 实时数据库中使用 firebase 唯一 uid 放置 UserDetails - How to put UserDeatils using firebase unique uid in Firebase Realtime Database 如何从您的 Android 应用程序检查 Firebase 存储中是否存在文件? - How to check if a file exists in Firebase storage from your android application? 如何检查该字段在Firebase中是否存在,如果存在则打印其值,如果不存在则在Flutter中添加一个空值 - How to check whether the field exists in Firebase, if it exists then print its value, if it does not exist, then add it with an empty value in Flutter 如何在 React Native 中从我的实时 Firebase 数据库中删除单个条目? - How to delete a single entry from my Realtime Firebase database in React Native? 连接到用户的唯一节点 - Firebase 实时数据库 - Unique node connected to user - Firebase Realtime Database 如何检查该字段是否存在于 firestore 中? - How to check if the field exists in firestore? 如何通过 clearData() 和 user_privacy.json 路径删除 firebase 实时数据库中某些条件为真的整个条目? - How to delete entire entry in firebase realtime database where some condition is true, through clearData() and user_privacy.json path? 将新属性更新到现有节点 firebase(实时数据库) - update new attribute into exists node firebase(realtime database) 将新属性更新到现有节点 firebase(实时数据库)(编辑) - update new attribute into exists node firebase(realtime database) (edit)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM