Emilis
Emilis

Reputation: 162

Firestore get doc without knowing the key

So I have this structure:

collection(users) { document(unknownID){ collection(listings) }, document(anotherUnknownId){ collection(listings) }, }

There will be many documents with unknownID how I should get those listings inside document with unknownID ?

const collectionRef = firestore().collection('users').doc(unknownID).collection('listings');

What to do with that unknownID, how to get every listings collection inside ? enter image description here

Upvotes: 0

Views: 1183

Answers (2)

Doug Stevenson
Doug Stevenson

Reputation: 317467

If you don't know the full path of the collection to query, then you won't be able to query it.

There are no workarounds: you will have to store the path somewhere so that your code can query it, or organize your data differently so that there are no unknowns when it comes time to query.

Upvotes: 2

Mangesh
Mangesh

Reputation: 5841

As documentation specifies:

When you delete a document, Cloud Firestore does not automatically delete the documents within its subcollections. You can still access the subcollection documents by reference.

Now since the document ids are auto-generated, you will need to have some mechanism to remember and identify those records. Following are the workarounds popping up in my head:

  1. Remember the complete path (or just the id of deleted document) somewhere else, so that it will be accessible to you.
  2. Don't delete the document, instead just mark it as deleted like isPermanantlyDeleted:true. This way you will be able to query only deleted documents.

Upvotes: 1

Related Questions