[英]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.