This is my Firebase Database structure. I want to check in Universities that new University which user will enter already exists or not? Problem is universities node have random key So I am not getting idea how can i check validity of Universities? If university already exists It should not write it again
-
chick that https://stackoverflow.com/questions/50900033/how-do-i-check-if-specific-child-value-exists-in-firebase-android – Abdulkarim Sep 12 '20 at 15:27
1 Answers
In your current structure you can check how often the university name already exists as a value under /Universities
with a query like this:
String name = "Comsats";
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Universities");
ref.orderByValue().equalTo(name).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.i("Firebase", "There are "+dataSnapshot.getChildrenCount()+" universities named "+name);
for (DataSnapshot universitySnapshot: dataSnapshot.getChildren()) {
Log.i("Firebase", universitySnapshot.getKey+": "+universitySnapshot.getValue(String.class));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
}
Or just to check if it exist at all, this would be slightly simpler/faster:
ref.orderByValue().equalTo(name).limitToFirst(1).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
Log.i("Firebase", "University "+name+" already exists");
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
}
If you use the above however to then add the university, you end up with a race condition. While one client is checking whether the name already exists, another might be adding it. Since the operations are not guaranteed to be in the order you'd like them, you could still end up with two universities with the same name.
The only way to prevent this is to use the university name as the key* of the nodes instead of as the value. Your structure would become:
"Universities": {
"University of Haripu": true,
"Comsats": true,
"UET": true
}
Node keys are guaranteed to be unique under a certain location, as there's no way to insert a second child node with the same name. So with this structure you automatically guarantee your uniqueness requirement in the data structure.
The true
values have no specific meaning in the above structure, and are just there because Firebase can't store a key without a value. If you have a more meaningful value, you can use that too. In fact, you may have to do that....
Firebase has a few characters that are not allowed to be present in a node key, which are allowed in the values. If your values may have such values, you'll want to perform some encoding from the value the user entered to the key that you store for the university.
Two common encodings are:
- Remove the offending characters from the string.
- Use the hashcode of the string.
The first approach leads to more recognizable keys, so I'll use that here. In practice the second approach is usually better and simpler in code, as hashing is pretty standard functionality these days while other encodings are likely going to be custom for your use-case or Firebase.
In both cases, you'll then want to store the actual value the user entered. So say that spaces and uppercase characters aren't allowed for keys (they are both allowed, so this is just an illustrative example), you'd end up with:
"Universities": {
"universityofharipu": "University of Haripu",
"comsats": "Comsats",
"uet": "UET"
}
Guaranteeing uniqueness has been covered quite a few times before, so I recommend checking out these:
- Firebase android : make username unique
- How to avoid duplicate data in Firebase Database
- Rule entries duplicates firebase not working, which contains links to more questions about uniqueness

- 565,676
- 79
- 828
- 807
-
Yes it worked . I changed positions ... I make key in as universities , but there is a little problem .... I want to calculate number of universities which are registered in database and when same university called again they call same key value and change the numbering , is there any solution ? – speed programmer Sep 12 '20 at 17:16
-
That's a new question, but it sounds like: 1) you'll want to store both the name and the count under each key, 2) then use `ServerValue.increment(1)` to increment the value. If you're having trouble making that work, I'd recommend posting a new question showing what you've tried, and where you are stuck. – Frank van Puffelen Sep 12 '20 at 18:49