Get a Firestore document using custom types

Get a Firestore document using custom types

Code sample

Go

dsnap, err := client.Collection("cities").Doc("BJ").Get(ctx)
if err != nil {
	return nil, err
}
var c City
dsnap.DataTo(&c)
fmt.Printf("Document data: %#v\n", c)

Java

DocumentReference docRef = db.collection("cities").document("BJ");
// asynchronously retrieve the document
ApiFuture<DocumentSnapshot> future = docRef.get();
// block on response
DocumentSnapshot document = future.get();
City city = null;
if (document.exists()) {
  // convert document to POJO
  city = document.toObject(City.class);
  System.out.println(city);
} else {
  System.out.println("No such document!");
}

Python

doc_ref = db.collection(u'cities').document(u'BJ')

doc = doc_ref.get()
city = City.from_dict(doc.to_dict())
print(city)

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser