Reputation:
I am using Cloud Firestore database in Swift iOS, I want to filter the list of root collection on the basis of values available in each documents' subcollection?
Root Collection --> Posts --> documents--> Subcollection--> document--> fieldvalue
The value of the "fieldvalue" has to decide the list of documents in root collection posts
I tried this below, but it is not working as required.
let postsRef = Firestore.firestore()
.collection("posts")
.document()
.collection("comments")
.whereField("comment_author_id", isEqualTo: Auth.auth().currentUser!.uid)
.whereField("l1", isEqualTo: true)
.limit(to: 50)
please assist
Upvotes: 3
Views: 808
Reputation: 138999
Your query is not working as required because when calling:
.document()
Right after:
.collection("posts")
Without passing any argument, Firestore doesn't really know which document you are reffering to. If you want to get all comments within your 2czkDrrg4gHgXbCID69u
document, then you should pass that id to the document()
function like this:
.collection("posts").document("2czkDrrg4gHgXbCID69u")
Remember, Firestore queries are shallow, can get comments only from the collection that the query is run against. A single query may only use properties of documents in a single collection.
If you want to get all comments within all your documents that exist in your posts
collection, then you should use a Firestore collection group query. Please note, that all subcollections must have the same (comments) name.
Upvotes: 1