简体   繁体   中英

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.

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.

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

I did it for userEmail's value... as I don't have any username other than the email.

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:

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.

Also returns true if this DataSnapshot contains any data. It is slightly more efficient than using DataSnapshot.getValue() !== null .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM