Android Cloud Firestore是从集合中获取少量文档的有效方法
从firebase collection获取3个文档(标记为红色)的最有效方法是什么?对我来说,另一个障碍是,该用户文档具有自动生成的Id。下面的代码可以工作,但我认为这不是有效的方法。如果“times”集合增长,那么在只需要一个或两个文档的情况下检索整个集合是没有意义的。 如果没有办法改进它,有什么建议可以修改我的firestore结构来实现这个目标吗Android Cloud Firestore是从集合中获取少量文档的有效方法,android,firebase,google-cloud-firestore,Android,Firebase,Google Cloud Firestore,从firebase collection获取3个文档(标记为红色)的最有效方法是什么?对我来说,另一个障碍是,该用户文档具有自动生成的Id。下面的代码可以工作,但我认为这不是有效的方法。如果“times”集合增长,那么在只需要一个或两个文档的情况下检索整个集合是没有意义的。 如果没有办法改进它,有什么建议可以修改我的firestore结构来实现这个目标吗 FirebaseFirestore db=FirebaseFirestore.getInstance(); 数据库收集(“用户”) .whe
FirebaseFirestore db=FirebaseFirestore.getInstance();
数据库收集(“用户”)
.whereEqualTo(“用户名”、“托莱克”)
.get().addOnCompleteListener(新的OnCompleteListener()){
@凌驾
未完成的公共void(@NonNull任务){
//任务-检索包含所有字段和集合的整个用户文档,我不想要什么-只想要文档Id
如果(!task.getResult().getDocuments().isEmpty()){
数据库收集(“用户”)
.document(task.getResult().getDocuments().get(0.getId())
.收集(“时间”)
.get().addOnCompleteListener(新的OnCompleteListener()){
@凌驾
未完成的公共void(@NonNull任务){
如果(!task.getResult().getDocuments().isEmpty()){
对于(DocumentSnapshot快照:task.getResult().getDocuments()){
Log.d(“标记”,“文档:”+snapshot.getId());
//在这里,我也收到整个“时代”收集,但只想要3个文件
}
}
}
});
}
}
});
要简化3次文档的加载,您可以:
这适用于多达10个值。如果您有10个以上的Id,则需要启动多个查询。您所说的“该用户文档具有自动生成的Id”是什么意思?你知道这个名字吗?除此之外?你有那些文件的身份证吗?20210225、20210226和20210227?用户文档的ID由Firebase随机生成。对于文档20210225、20210226和20210227,我有ID。如何“用户文档的ID由Firebase随机生成”?为什么不使用总是知道UID?这些用户是由主要的、经过身份验证的用户添加的,不需要进行身份验证。我指的Firestore文档的自动生成ID-如果不通过创建设置ID,则自动生成。工作正常,不会加载整个集合。谢谢
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("users")
.whereEqualTo("userName", "tolek")
.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
// task - retrieves entire user document with all fields and collections, what I don't want - want only document Id
if (!task.getResult().getDocuments().isEmpty()) {
db.collection("users")
.document(task.getResult().getDocuments().get(0).getId())
.collection("times")
.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (!task.getResult().getDocuments().isEmpty()) {
for (DocumentSnapshot snapshot : task.getResult().getDocuments()) {
Log.d("TAG", "Documents : " + snapshot.getId());
//here also I receive whole "times" collection but want only 3 documents
}
}
}
});
}
}
});
CollectionReference timesRef = db.collection("users")
.document(task.getResult().getDocuments().get(0).getId())
.collection("times");
timesRef.whereIn(FieldPath.documentId(), Arrays.asList("20210225", "20210226", "20210227"));