0PricingLogin
React Native Academy · Lesson

Reading and Writing Firestore Documents

Add, read, update, and delete documents in a Firestore collection using the Firestore module, structure data with subcollections, and apply security rules.

Firestore Data Model

Cloud Firestore is a NoSQL document database. Data is organized in collections (like folders) that contain documents (like JSON files). Each document has a unique ID and a set of key-value fields. Documents can contain sub-collections for nested data.

For example, a posts collection contains many post documents, each with fields like title, content, and userId. A post document can have a nested comments sub-collection for its replies. This model scales differently from SQL — think about access patterns first, then model data accordingly.

Getting a Collection Reference

Access a Firestore collection using firestore().collection('collection-name'). This returns a CollectionReference that you use to read documents, add new ones, or listen for changes. Collection references are lightweight and do not trigger any network calls on their own.

To reference a specific document within a collection, chain .doc('document-id'). If you want Firestore to generate a random ID, omit the argument when creating a new document with .add().

import firestore from '@react-native-firebase/firestore';

// Reference to the 'posts' collection
const postsRef = firestore().collection('posts');

// Reference to a specific post document
const postRef = firestore().collection('posts').doc('post-id-123');

// Reference to a sub-collection
const commentsRef = firestore()
  .collection('posts')
  .doc('post-id-123')
  .collection('comments');

All lessons in this course

  1. Connecting React Native Firebase to Your Project
  2. Email and Phone Authentication with Firebase
  3. Reading and Writing Firestore Documents
  4. Real-Time Listeners and Offline Persistence
← Back to React Native Academy