Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

firebase - Get parents from Firestore Collection Group Query, WITHOUT RETRIEVING SONS

I am designing a Firestore Database and am wondering about the cost implications of the following architecture...

Following cloud Firestore tutorials, lets imagine this architecture: A 'Restaurants' Collection has a 'Reviews' subcollection. Something like:

--Restuarants
   --My Restaurant
      --Reviews
         --My Review 1, through n

I am interested in querying the reviews subcollection, but I am not really interested in the review itself, but on the restaurants.

So, for instance, in a query like Get all reviews posted on August 1st 2019 I am actually interested in the restaurants that have at least one review posted on August 1st 2019. ie. I would like to get back the restaurants' documents, not the reviews.

From this post and this it seems like my implementation would probably look something like this:

    QuerySnapshot myQuerySnapshot = await Firestore.instance.collectionGroup('reviews')
                .where('reviewDate', isEqualTo: '20190801').snapshots().first;
    List<DocumentSnapshot> myDocumentSnapshots = myQuerySnapshot.documents;

    myDocumentSnapshots.asMap().forEach((index, son) {
          snapshot.reference.parent().parent().get().then((parent) {
            print("Printing snapshot: $index");
            print("son id: "+son.documentID);
            print("parent id: "+parent.documentID); //I could then retrieve the parent data.
            });
    });

With this implementation, I would be fetching all the 'sons' (ie. all the reviews). This has a couple of disadvantages:

  • Cost. A restaurant may have, say, 1000 reviews posted on august 1st 2019; I would be charged for 1001 reads (1000 reviews + the actual parent document I am interested in).
  • Data consumption. My users would retrieve way more data from the servers than what is actually needed.

Is there a better way to do this? Is there a way to avoid fetching the reviews and getting only the parent documents?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...