[英]how to search at firestore document's field
How to search at firestore documents? 如何在firestore文件中搜索? if firestore collection contains certain document and if there has a string field at document named 'title'. 如果firestore集合包含某些文档,并且在文档中有一个名为“title”的字符串字段。 How can i search specific title using firebase android api. 如何使用firebase android api搜索特定标题。
It is documented in the Docs here , in the last section of the page, titled Get multiple documents from a collection . 它在文档记录在这里 ,在页面的最后一节,标题为获取从集合多个文档 。
Firestore provides a whereEqualTo
function to query your data. Firestore提供了whereEqualTo
函数来查询您的数据。
Example code (from Docs): 示例代码(来自Docs):
db.collection("cities")
.whereEqualTo("capital", true) // <-- This line
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
Log.d(TAG, document.getId() + " => " + document.getData());
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
I have use MutableLiveData for search the specific user's name .where i pass the user's name and check whether it is available in Firestore or not 我使用MutableLiveData搜索特定用户的名字。我传递用户的名字并检查它是否在Firestore中可用
Here is my code:- 这是我的代码: -
public MutableLiveData<UsersModel> getSpecificUser(final String name) {
final MutableLiveData<UsersModel> usersData = new MutableLiveData<>();
db.collection("users")
.whereEqualTo("name", name)
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot snapshot, @Nullable FirebaseFirestoreException e) {
if(e!=null || snapshot.size()==0){
Toast.makeText(activity, "User not found", Toast.LENGTH_SHORT).show();
}
for (DocumentChange userDoc : snapshot.getDocumentChanges()) {
UsersModel user = userDoc.getDocument().toObject(UsersModel.class);
if (user.name != null) {
if (userDoc.getType() == DocumentChange.Type.ADDED || userDoc.getType() == DocumentChange.Type.MODIFIED) {
usersData.setValue(user);
}
}
}
}
});
return usersData;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.