Create a Firestore subcollection reference

Stay organized with collections Save and categorize content based on your preferences.

Create a Firestore subcollection reference

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

Go

messageRef := client.Collection("rooms").Doc("roomA").
	Collection("messages").Doc("message1")

Java

// Reference to a document in subcollection "messages"
DocumentReference document =
    db.collection("rooms").document("roomA").collection("messages").document("message1");

Node.js

const messageRef = db.collection('rooms').doc('roomA')
  .collection('messages').doc('message1');

PHP

$document = $db
    ->collection('rooms')
    ->document('roomA')
    ->collection('messages')
    ->document('message1');

Python

room_a_ref = db.collection(u'rooms').document(u'roomA')
message_ref = room_a_ref.collection(u'messages').document(u'message1')

Ruby

message_ref = firestore.col("rooms").doc("roomA").col("messages").doc("message1")

What's next

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