0

So what I'm trying to do is search from 2 or more collection. 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. 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. Is it possible to achieve? or my logic is totally wrong

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Aldy Yuan
  • 1,795
  • 9
  • 23
  • Sure, it sounds possible. You will execute a query for each collection until you find what you're looking for. – Doug Stevenson May 06 '20 at 05:48
  • Yea I'm just a bit confused because I'm new to this noSQL things. So basically if it return null, it will execute another query right? until it found what user looking for. Thanks for commenting tho, I might delete this question later lmao – Aldy Yuan May 06 '20 at 06:03
  • Your sub/collections are named Tier-n or they have the same name? – gstvg May 06 '20 at 06:07
  • Tier-n because maybe I could loop it and then after it found what user looking for, I'll just break it. Will it work? – Aldy Yuan May 06 '20 at 06:40

1 Answers1

1

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

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • If i use isGreaterThanOrEqualTo:, will it work just fine?, or there will be perfomance issues or etc . I'm sorry Firestore is new thing for me – Aldy Yuan May 06 '20 at 09:05
  • isGreaterThanOrEqualTo is different than isequalto, firebase uses the `lexicographic ordering` for strings for explanation: https://stackoverflow.com/a/38226376/7015400, so you have to check if you are doing it correctly or no, but regarding performance issue its the same as isequalto you are querying the collection – Peter Haddad May 06 '20 at 09:16
  • 1
    for tutorial firebase with flutter, check my blog https://petercoding.com – Peter Haddad May 06 '20 at 10:45