user13027438
user13027438

Reputation: 25

Firestore Firebase make Query by documentID, swift

So I have a collection called users. I want to be able to search if a user exists and if so display its documents.

I've tried 2 options which both don't seem to work

  db.collection("users").whereField(DocumentID == "max")
            .getDocuments()

second way

db.collection("users").whereField("Max", isEqualTo: true)
            .getDocuments()

please find image of my database attached here

Upvotes: 1

Views: 687

Answers (1)

user12766300
user12766300

Reputation:

Try this:

let db = Firestore.firestore()

let docRef = db.collection("collectionName").document("documentName")

docRef.getDocument { (document, error) in
    if let document = document, document.exists {
        let password = document.get("fieldName")
    } else {
        print("Document does not exist")
    }
}

Edit: Looking at your code, I think you are mixing up collections, documents and fields.

The collection in your case is "users", the documents are the second column in your image, eg. Arthur, Fredrick etc. Within the document of say Arthur, is the field: password. So it goes collection → document → field.

Upvotes: 4

Related Questions