简体   繁体   English

如何查找 Firebase 中是否存在在其字段中具有特定价值的孩子?

[英]How to find if a child with particular value in its fields exists in Firebase?

I need to find if a child with particular value in one of its fields exists in Firebase.我需要查找 Firebase 中是否存在在其字段之一中具有特定值的子项。

Below is my database structure:下面是我的数据库结构:

数据库结构的图像

To check if a field exists,you can do this:要检查一个字段是否存在,你可以这样做:

  DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("bdxhATalcDRJCiZZcUFUJo3yrJF2");
  ref.addValueEventListener(new ValueEventListener(){
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
   if(dataSnapshot.exists()){
        //then it exists
  }

  @Override
  public void onCancelled(FirebaseError firebaseError) {


 }
 });

This will check if child("bdxhATalcDRJCiZZcUFUJo3yrJF2") exists in the database.这将检查数据库中是否存在child("bdxhATalcDRJCiZZcUFUJo3yrJF2")

If you do this:如果你这样做:

ref=FirebaseDatabase.getInstance().getReference().child("bdxhATalcDRJCiZZcUFUJo3yrJF2").orderByChild("chat_id").equalTo(chatid);

then it will check if that child also exists in the database然后它将检查该孩子是否也存在于数据库中

postRef = FirebaseDatabase.getInstance().getReference().child("bdxhATalcDRJCiZZcUFUJo3yrJF2");

    postRef.orderByChild("chat_id").equalTo(chat_id)
        .addListenerForSingleValueEvent(new ValueEventListener() {

           @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if(dataSnapshot.exists()){
                   //bus number exists in Database
            } else {
                //bus number doesn't exists.
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });

In your case you would set up query like this:在您的情况下,您将像这样设置查询:

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

        Query query = reference.orderByChild("chat_id").equalTo("CHAT_ID_To_COMPARE");

        query.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {

                    for (DataSnapshot issue : dataSnapshot.getChildren()) {
                        // do something with the individual object
                    }
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

This would filter your data at server side and no need to filter data at app side.这将在服务器端过滤您的数据,而无需在应用程序端过滤数据。

etEmail is an EditText etEmail 是一个 EditText

I did it for userEmail's value... as I don't have any username other than the email.我这样做是为了 userEmail 的价值......因为除了电子邮件之外我没有任何用户名。

for a structure like this...对于这样的结构...

"users": {
    pushId: {
        "userEmail": {

        }
    }
}

onCreate创建时

etEmail.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(final CharSequence s, int start, int before, int count) {
                //yourFunction();
                DatabaseReference databaseUsers = FirebaseDatabase.getInstance().getReference().child("users");
                databaseUsers.orderByChild("userEmail").equalTo(String.valueOf(s))
                        .addListenerForSingleValueEvent(new ValueEventListener() {

                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                if (dataSnapshot.exists()) {
                                    for (DataSnapshot issue : dataSnapshot.getChildren()) {
                                            // do something with the individual object
                                            //User email already exists
                                            //do something

                                            //return; stops the function from executing further
                                            return;
                                    }
                                } else {
                                // email doesn't exists.
                                // do something

                                }
                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError databaseError) {

                            }
                        });
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

When you do the reference and you have your DataSnapshot , you can check it like this:当你做参考并且你有你的DataSnapshot时,你可以这样检查它:

yourref.child("chat_id").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (!dataSnapshot.exists()) {
            //Do something
        } 
    }

exists()存在()

Returns true if the snapshot contains a non-null value.如果快照包含非空值,则返回 true。

Also returns true if this DataSnapshot contains any data.如果此DataSnapshot包含任何数据,也会返回 true。 It is slightly more efficient than using DataSnapshot.getValue() !== null .它比使用DataSnapshot.getValue() !== null稍微高效一些。

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

相关问题 如何检查该字段在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 Firebase:检查值是否已存在于任何子节点中 - Firebase: Checking whether a value already exists in any of the child nodes or not 如何在 Firebase 中搜索 Child 的值 - How to search a value of a Child in Firebase Firebase 查询child of child是否包含值 - Firebase query if child of child contains a value React-Native Real-Time Firebase 如何找到所有子组件(所有引用)? - React-Native Real-Time Firebase How to find all child components(all references)? 如何检查 Firebase 集合中是否已经存在特定 ID - How to check If particular Id already in Firebase collection 如何在 firebase android 中获取子数据的子数据 - how to get child of child of child data in firebase android 如何用 python 检测 firebase child 的变化? - How to detect changes in firebase child with python? 如何检查 firebase 数据库中是否存在唯一条目? - How to check if unique entry exists in firebase database or not? 如何从您的 Android 应用程序检查 Firebase 存储中是否存在文件? - How to check if a file exists in Firebase storage from your android application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM