Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

ios - Get All Documents for a Sub Collection in Firebase Cloud Firestore

Update

For anybody who also is having similar problem in the future. I am not sure why the ObjectiveC-SDK with documentWithPath and documentWithCollection is not working but what i have found is if we directly give the path it can work.

So instead of asking

self.db collectionWithPath:@"stage_tickets"]documentWithPath:bookingReference]collectionWithPath:@"tickets"]getDocumentsWithCompletion

and getting nothing in return you can directly write the path

self.db collectionWithPath:@"stage_tickets/{bookingReference}/tickets"]getDocumentsWithCompletion


I have a Cloud FireStore database with a structure like this.

Cloud Firestore Database

The Structure is basically stage_tickets(Collection)->Numbers(Documents)->Tickets(Collection)->Numbers(Documents)

The really Strange thing is if i want to get all the numbers from the first level i can easily get them via

[[self.db collectionWithPath:@"stage_tickets"]
          getDocumentsWithCompletion:^(FIRQuerySnapshot *snapshot, NSError *error) {
                      if (error != nil) {
                        NSLog(@"Error getting documents: %@", error);
                      } else {
                        for (FIRDocumentSnapshot *document in snapshot.documents) {
                          NSLog(@"%@ => %@", document.documentID, document.data);
                        }
                      }
}];

But if i try to reach the second level of numbers via

[[[[self.db collectionWithPath:@"stage_tickets"]documentWithPath:bookingReference]collectionWithPath:@"tickets"]getDocumentsWithCompletion:^(FIRQuerySnapshot * _Nullable snapshot, NSError * _Nullable error) {
                    if(error!=nil){
                        NSLog(@"error in getting QR Code");
                    }else{
                        for (FIRDocumentSnapshot *document in snapshot.documents) {
                            NSLog(@"%@ => %@", document.documentID, document.data);
                        }
                    }
}];

All i get is empty documents in Snapshot. Can anybody suggest what is wrong i am doing here, maybe some behaviour of fireStore that i am unaware of and how can i fix this.

question from:https://stackoverflow.com/questions/66057216/get-all-documents-for-a-sub-collection-in-firebase-cloud-firestore

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

1 Answer

0 votes
by (71.8m points)

You can get all ticket's document data using CollectionGroup like as below

self.db.collectionGroup("tickets").getDocuments { (snapshot, error) in
        
        if let err = error
        {
            print(err)
        }
        else
        {
            if let snapshot = snapshot,snapshot.documents.count > 0
            {
                for document in snapshot.documents
                {
                    print(document.data())
                }
            }
            
        }
    }

You can get all documents for particular collection as below

self.db.collection("stage_tickets").document("00005055").collection("tickets").getDocuments { (snapshot, error) in
        
        if let err = error
        {
            print(err)
        }
        else
        {
            if let snapshot = snapshot,snapshot.documents.count > 0
            {
                for document in snapshot.documents
                {
                    print(document.data())
                }
            }
            
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...