LoginSignup
1
2

More than 3 years have passed since last update.

[swift×Firebase]ドキュメントIDから単体情報をクエリする方法

Posted at

やりたかったこと

ドキュメントIDを使用して、各データにアクセスしたかった。
今回は、複数ではなく単体のデータさえ返って来ればよかった。

やったこと

以下のようにドキュメントIDはクエリ条件に設定することが可能。

「.whereField(FieldPath.documentID(), in: [documentid])」

func getMemo(documentid:String){

        let db = Firestore.firestore()

        db.collection("memoes")
            .whereField(FieldPath.documentID(),  in: [documentid])
                .getDocuments(){ (querySnapshot, err) in
                if let error = err{
                    print("error: \(error.localizedDescription)")
                    return
                }
                guard let snapshot = querySnapshot else{
                    return
                }
                for document in snapshot.documents{
                    print("ドキュメントタイトル:\(document.get("title") as! String)")
                }
            }
    }

これでできた。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2