Home > Mobile >  Firebase Firestore query by document id using swift
Firebase Firestore query by document id using swift

Time:04-16

I am trying to query my database to get all the users where the document id is equal to the users id. I know that I can add that id as a field into the document, but I'd like to optimize the memory I take up, so I'd prefer accessing the document id directly through. Something like

.whereField(DOCUMENT_ID, isEqualTo, User.id)

DOCUMENT_ID is not a valid field so far as I know, and I don't know the equivalent. I have read around that other languages modules have a workaround like .whereField(Firebase.firestore.FieldValues.documentId(), isEqualTo, User.id)

but I've heard hide nor hair of that in swiftUI. Any ideas?

CodePudding user response:

To filter on the document ID in a query, you can use FieldPath.documentID() in Swift too: https://firebase.google.com/docs/reference/swift/firebasefirestore/api/reference/Classes/FieldPath#/c:objc(cs)FIRFieldPath(cm)documentID. But if you do this in a single document, that'd be the same as just doing document(User.id).

  • Related