Cloud Firestore Data model Stay organized with collections Save and categorize content based on your preferences.
Cloud Firestore is a NoSQL, document-oriented database. Unlike a SQL database,there are no tables or rows. Instead, you store data indocuments, which areorganized intocollections.
Eachdocument contains a set of key-value pairs.Cloud Firestore isoptimized for storing large collections of small documents.
All documents must be stored in collections. Documents can containsubcollections and nested objects, both of which can include primitive fieldslike strings or complex objects like lists.
Collections and documents are created implicitly inCloud Firestore.Simply assign data to a document within a collection. If either the collectionor document does not exist,Cloud Firestore creates it.
Documents
InCloud Firestore, the unit of storage is the document. A document is alightweight record that contains fields, which map to values. Each document isidentified by a name.
A document representing a useralovelace
might look like this:
alovelace
first : "Ada"
last : "Lovelace"
born : 1815
Complex, nested objects in a document are called maps. For example, you couldstructure the user's name from the example above with a map, like this:
alovelace
name :
first : "Ada"
last : "Lovelace"
born : 1815
You may notice that documents look a lot like JSON. In fact, they basically are.There are some differences (for example, documents support extra data types andare limited in size to 1 MB), but in general, you can treat documents aslightweight JSON records.
Collections
Documents live in collections, which are simply containers for documents. Forexample, you could have ausers
collection to contain your various users, eachrepresented by a document:
users
alovelace
first : "Ada"
last : "Lovelace"
born : 1815
aturing
first : "Alan"
last : "Turing"
born : 1912
Cloud Firestore is schemaless, so you have complete freedom over whatfields you put in each document and what data types you store in those fields.Documents within the same collection can all contain different fields or storedifferent types of data in those fields. However, it's a good idea to use thesame fields and data types across multiple documents, so that you can query thedocuments more easily.
A collection contains documents and nothing else. It can't directly contain rawfields with values, and it can't contain other collections. (SeeHierarchicalData for an explanation of how to structure more complexdata inCloud Firestore.)
The names of documents within a collection are unique. You can provide your ownkeys, such as user IDs, or you can letCloud Firestore create random IDsfor you automatically.
You do not need to "create" or "delete" collections. After you create the firstdocument in a collection, the collection exists. If you delete all of thedocuments in a collection, it no longer exists.
References
Every document inCloud Firestore is uniquely identified by its locationwithin the database. The previous example showed a documentalovelace
withinthe collectionusers
. To refer to this location in your code, you can create areference to it.
Web
import{doc}from"firebase/firestore";constalovelaceDocumentRef=doc(db,'users','alovelace');
Web
Learn more about the tree-shakeable modular Web API and its advantages over the namespaced API.varalovelaceDocumentRef=db.collection('users').doc('alovelace');
Swift
letalovelaceDocumentRef=db.collection("users").document("alovelace")
Objective-C
FIRDocumentReference*alovelaceDocumentRef=[[self.dbcollectionWithPath:@"users"]documentWithPath:@"alovelace"];
Kotlin
valalovelaceDocumentRef=db.collection("users").document("alovelace")
Java
DocumentReferencealovelaceDocumentRef=db.collection("users").document("alovelace");
Dart
finalalovelaceDocumentRef=db.collection("users").doc("alovelace");
Java
// Reference to a document with id "alovelace" in the collection "users"DocumentReferencedocument=db.collection("users").document("alovelace");
Python
a_lovelace_ref=db.collection("users").document("alovelace")
Python
a_lovelace_ref=db.collection("users").document("alovelace")
C++
DocumentReferencealovelace_document_reference=db->Collection("users").Document("alovelace");
Node.js
constalovelaceDocumentRef=db.collection('users').doc('alovelace');
Go
import("cloud.google.com/go/firestore")funccreateDocReference(client*firestore.Client){alovelaceRef:=client.Collection("users").Doc("alovelace")_=alovelaceRef}
PHP
PHP
For more on installing and creating aCloud Firestore client, refer toCloud Firestore Client Libraries.
$document = $db->collection('samples/php/users')->document('alovelace');
Unity
DocumentReferencedocumentRef=db.Collection("users").Document("alovelace");
C#
C#
For more on installing and creating aCloud Firestore client, refer toCloud Firestore Client Libraries.
DocumentReferencedocumentRef=db.Collection("users").Document("alovelace");
Ruby
document_ref=firestore.col("users").doc("alovelace")
A reference is a lightweight object that just points to a location in yourdatabase. You can create a reference whether or not data exists there, andcreating a reference does not perform any network operations.
You can also create references tocollections:
Web
import{collection}from"firebase/firestore";constusersCollectionRef=collection(db,'users');
Web
Learn more about the tree-shakeable modular Web API and its advantages over the namespaced API.varusersCollectionRef=db.collection('users');
Swift
letusersCollectionRef=db.collection("users")
Objective-C
FIRCollectionReference*usersCollectionRef=[self.dbcollectionWithPath:@"users"];
Kotlin
valusersCollectionRef=db.collection("users")
Java
CollectionReferenceusersCollectionRef=db.collection("users");
Dart
finalusersCollectionRef=db.collection("users");
Java
// Reference to the collection "users"CollectionReferencecollection=db.collection("users");
Python
users_ref=db.collection("users")
Python
users_ref=db.collection("users")
C++
CollectionReferenceusers_collection_reference=db->Collection("users");
Node.js
constusersCollectionRef=db.collection('users');
Go
import("cloud.google.com/go/firestore")funccreateCollectionReference(client*firestore.Client){usersRef:=client.Collection("users")_=usersRef}
PHP
PHP
For more on installing and creating aCloud Firestore client, refer toCloud Firestore Client Libraries.
$collection = $db->collection('samples/php/users');
Unity
CollectionReferencecollectionRef=db.Collection("users");
C#
C#
For more on installing and creating aCloud Firestore client, refer toCloud Firestore Client Libraries.
CollectionReferencecollectionRef=db.Collection("users");
Ruby
collection_ref=firestore.col"users"
For convenience, you can also create references by specifying the path to adocument or collection as a string, with path components separated by a forwardslash (/
). For example, to create a reference to thealovelace
document:
Web
import{doc}from"firebase/firestore";constalovelaceDocumentRef=doc(db,'users/alovelace');
Web
Learn more about the tree-shakeable modular Web API and its advantages over the namespaced API.varalovelaceDocumentRef=db.doc('users/alovelace');
Swift
letaLovelaceDocumentReference=db.document("users/alovelace")
Objective-C
FIRDocumentReference*aLovelaceDocumentReference=[self.dbdocumentWithPath:@"users/alovelace"];
Kotlin
valalovelaceDocumentRef=db.document("users/alovelace")
Java
DocumentReferencealovelaceDocumentRef=db.document("users/alovelace");
Dart
finalaLovelaceDocRef=db.doc("users/alovelace");
Java
// Reference to a document with id "alovelace" in the collection "users"DocumentReferencedocument=db.document("users/alovelace");
Python
a_lovelace_ref=db.document("users/alovelace")
Python
a_lovelace_ref=db.document("users/alovelace")
C++
DocumentReferencealovelace_document=db->Document("users/alovelace");
Node.js
constalovelaceDocumentRef=db.doc('users/alovelace');
Go
import("cloud.google.com/go/firestore")funccreateDocReferenceFromString(client*firestore.Client){// Reference to a document with id "alovelace" in the collection "users"alovelaceRef:=client.Doc("users/alovelace")_=alovelaceRef}
PHP
PHP
For more on installing and creating aCloud Firestore client, refer toCloud Firestore Client Libraries.
$document = $db->document('users/alovelace');
Unity
DocumentReferencedocumentRef=db.Document("users/alovelace");
C#
C#
For more on installing and creating aCloud Firestore client, refer toCloud Firestore Client Libraries.
DocumentReferencedocumentRef=db.Document("users/alovelace");
Ruby
document_path_ref=firestore.doc"users/alovelace"
Hierarchical Data
To understand how hierarchical data structures work inCloud Firestore,consider an example chat app with messages and chat rooms.
You can create a collection calledrooms
to store different chat rooms:
rooms
roomA
name : "my chat room"
roomB
...
Now that you have chat rooms, decide how to store your messages. You might notwant to store them in the chat room's document. Documents inCloud Firestoreshould be lightweight, and a chat room could contain a large number of messages.However, you can create additional collections within your chat room's document,as subcollections.
Subcollections
The best way to store messages in this scenario is by using subcollections. Asubcollection is a collection associated with a specific document.
Note: You can query across subcollections with the same collection ID by usingCollection Group Queries.You can create a subcollection calledmessages
for every room document inyourrooms
collection:
rooms
roomA
name : "my chat room"
messages
message1
from : "alex"
msg : "Hello World!"
message2
...
roomB
...
In this example, you would create a reference to a message in the subcollectionwith the following code:
Web
import{doc}from"firebase/firestore";constmessageRef=doc(db,"rooms","roomA","messages","message1");
Web
Learn more about the tree-shakeable modular Web API and its advantages over the namespaced API.varmessageRef=db.collection('rooms').doc('roomA').collection('messages').doc('message1');
Swift
letmessageRef=db.collection("rooms").document("roomA").collection("messages").document("message1")
Objective-C
FIRDocumentReference*messageRef=[[[[self.dbcollectionWithPath:@"rooms"]documentWithPath:@"roomA"]collectionWithPath:@"messages"]documentWithPath:@"message1"];
Kotlin
valmessageRef=db.collection("rooms").document("roomA").collection("messages").document("message1")
Java
DocumentReferencemessageRef=db.collection("rooms").document("roomA").collection("messages").document("message1");
Dart
finalmessageRef=db.collection("rooms").doc("roomA").collection("messages").doc("message1");
Java
// Reference to a document in subcollection "messages"DocumentReferencedocument=db.collection("rooms").document("roomA").collection("messages").document("message1");
Python
room_a_ref=db.collection("rooms").document("roomA")message_ref=room_a_ref.collection("messages").document("message1")
Python
room_a_ref=db.collection("rooms").document("roomA")message_ref=room_a_ref.collection("messages").document("message1")
C++
DocumentReferencemessage_reference=db->Collection("rooms").Document("roomA").Collection("messages").Document("message1");
Node.js
constmessageRef=db.collection('rooms').doc('roomA').collection('messages').doc('message1');
Go
import("cloud.google.com/go/firestore")funccreateSubcollectionReference(client*firestore.Client){messageRef:=client.Collection("rooms").Doc("roomA").Collection("messages").Doc("message1")_=messageRef}
PHP
PHP
For more on installing and creating aCloud Firestore client, refer toCloud Firestore Client Libraries.
$document = $db ->collection('rooms') ->document('roomA') ->collection('messages') ->document('message1');
Unity
DocumentReferencedocumentRef=db.Collection("Rooms").Document("RoomA").Collection("Messages").Document("Message1");
C#
C#
For more on installing and creating aCloud Firestore client, refer toCloud Firestore Client Libraries.
DocumentReferencedocumentRef=db.Collection("Rooms").Document("RoomA").Collection("Messages").Document("Message1");
Ruby
message_ref=firestore.col("rooms").doc("roomA").col("messages").doc("message1")
Notice the alternating pattern of collections and documents. Your collectionsand documents must always follow this pattern. You cannot reference a collectionin a collection or a document in a document.
Subcollections allow you to structure data hierarchically, making data easier toaccess. To get all messages inroomA
, you can create a collection referenceto the subcollectionmessages
and interact with it like you would any othercollection reference.
Documents in subcollections can contain subcollections as well, allowing you tofurther nest data. You can nest data up to 100 levels deep.
Warning: Deleting a document does not delete its subcollections!Whenyou delete a document that has subcollections, those subcollections arenot deleted. For example, there may be a document located at
coll/doc/subcoll/subdoc
even though the documentcoll/doc
no longer exists. If you want to deletedocuments in subcollections when deleting a parent document, you must do so manually,as shown inDeleteCollections.Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-10-01 UTC.