0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Go】自動生成されたdocument idを取得したい【Firestore】

Last updated at Posted at 2021-11-17

Goにて、Firestoreのデータをクエリで引っ張ってくるときに、document idを取ってくる方法がなかなか見つからなかったので、自分の備忘録です。

#結論
iter.Next()した結果は「DocumentSnapshot」になる。
doc.Ref.ID でdocument idを取ってこれる。

iter := client.Collection("activities").Where("Date", "==", v).Documents(ctx)

var result []interface{}
for {
	doc, err := iter.Next()
	if err == iterator.Done {
		break
	}
	if err != nil {
		fmt.Fprint(w, err)
		return
	}
	fmt.Println(doc.Ref.ID)
	m := doc.Data()
	m["id"] = doc.Ref.ID
	result = append(result, m)
}

#参考
https://pkg.go.dev/cloud.google.com/go/firestore#DocumentSnapshot
https://pkg.go.dev/cloud.google.com/go/firestore#DocumentRef

0
0
1

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?