[英]Flutter Firestore search from more than 1 collection
So what I'm trying to do is search from 2 or more collection.所以我想做的是从 2 个或更多集合中搜索。 Here's my collection:
这是我的收藏:
Tier-1
Tier-2
Tier-3
Let's say I want to search with key "name" and what I want to achieve is, it will searching from Tier-1 to Tier-3.假设我想用键“名称”进行搜索,而我想要实现的是,它将从第 1 层搜索到第 3 层。 If there's no document equal to "name" from Tier-1, it will continue to Tier-2, if there's document equal to "name" at Tier-2, it will stop and return the document.
如果Tier-1没有与“name”相等的文件,它将继续到Tier-2,如果在Tier-2有与“name”相等的文件,它将停止并返回该文件。 Is it possible to achieve?
有没有可能实现? or my logic is totally wrong
或者我的逻辑完全错误
Basically what you are saying, is that you have the following structure:基本上你要说的是,你有以下结构:
Tier-1 (collection) -> docId(document) -> Tier-2 (collection) -> docId(document) -> Tier-3 (collection) -> docId(document)
You can do the following:您可以执行以下操作:
void getData() {
Firestore.instance
.collection("Tier-1")
.where("name", isEqualTo: "peter")
.getDocuments()
.then((querySnapshot) {
querySnapshot.documents.forEach((result) {
if (result.exists) {
print(result.data);
} else {
Firestore.instance
.collectionGroup("Tier-2")
.where("name", isEqualTo: "peter")
.getDocuments()
.then((querySnapshot) {
querySnapshot.documents.forEach((result) {
if (result.exists) {
print(result.data);
} else {
Firestore.instance
.collectionGroup("Tier-3")
.where("name", isEqualTo: "peter")
.getDocuments()
.then((querySnapshot) {
querySnapshot.documents.forEach((result) {
if (result.exists) {
print(result.data);
} else {}
});
});
}
});
});
}
});
});
}
First you check the top collection Tier-1
, if it returns no result, then you need to check the subcollection Tier-2
首先检查顶部集合
Tier-1
,如果没有返回结果,则需要检查子集合Tier-2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.