React Native Firebase Cloud Firestore provides a NoSQL document database with real-time synchronization capabilities.
—
Core database functionality for accessing collections and documents, with full CRUD operations and real-time synchronization.
Get the default Firestore instance or initialize with specific database ID.
/**
* Get the default Firestore instance
* @returns Firestore module instance
*/
firestore(): FirebaseFirestoreTypes.Module;
/**
* Get Firestore instance for specific database
* @param databaseId - Optional database identifier
* @returns Firestore module instance
*/
firestore(databaseId?: string): FirebaseFirestoreTypes.Module;Usage Examples:
import firestore from '@react-native-firebase/firestore';
// Default database
const db = firestore();
// Specific database
const customDb = firestore('custom-database');Access collections within the Firestore database.
/**
* Get a reference to a collection
* @param collectionPath - Path to the collection
* @returns CollectionReference instance
*/
collection(collectionPath: string): FirebaseFirestoreTypes.CollectionReference;
/**
* Query across collection groups with the same ID
* @param collectionId - Collection identifier to query across
* @returns Query instance for collection group
*/
collectionGroup(collectionId: string): FirebaseFirestoreTypes.Query;
interface CollectionReference<T = FirebaseFirestoreTypes.DocumentData> extends Query<T> {
readonly id: string;
readonly parent: FirebaseFirestoreTypes.DocumentReference | null;
readonly path: string;
/**
* Add a new document to the collection
* @param data - Document data to add
* @returns Promise resolving to the new document reference
*/
add(data: T): Promise<FirebaseFirestoreTypes.DocumentReference<T>>;
/**
* Get a document reference within this collection
* @param documentPath - Optional document path
* @returns Document reference
*/
doc(documentPath?: string): FirebaseFirestoreTypes.DocumentReference<T>;
}Usage Examples:
import firestore from '@react-native-firebase/firestore';
// Get collection reference
const usersCollection = firestore().collection('users');
// Add new document
const newUser = await usersCollection.add({
name: 'John Doe',
email: 'john@example.com',
createdAt: firestore.FieldValue.serverTimestamp()
});
// Get document reference
const userDoc = usersCollection.doc('userId');
// Collection group query across all 'messages' subcollections
const allMessages = firestore().collectionGroup('messages');Access and manipulate individual documents within collections.
/**
* Get a reference to a document
* @param documentPath - Path to the document
* @returns DocumentReference instance
*/
doc(documentPath: string): FirebaseFirestoreTypes.DocumentReference;
interface DocumentReference<T = FirebaseFirestoreTypes.DocumentData> {
readonly firestore: FirebaseFirestoreTypes.Module;
readonly id: string;
readonly parent: FirebaseFirestoreTypes.CollectionReference<T>;
readonly path: string;
/**
* Get a subcollection reference
* @param collectionPath - Path to the subcollection
* @returns CollectionReference for the subcollection
*/
collection(collectionPath: string): FirebaseFirestoreTypes.CollectionReference;
/**
* Delete the document
* @returns Promise resolving when deletion completes
*/
delete(): Promise<void>;
/**
* Get the document data
* @param options - Optional get options
* @returns Promise resolving to document snapshot
*/
get(options?: FirebaseFirestoreTypes.GetOptions): Promise<FirebaseFirestoreTypes.DocumentSnapshot<T>>;
/**
* Set the document data (overwrites existing data)
* @param data - Document data to set
* @param options - Optional set options for merging
* @returns Promise resolving when set completes
*/
set(data: T, options?: FirebaseFirestoreTypes.SetOptions): Promise<void>;
/**
* Update specific fields in the document
* @param data - Partial data to update
* @returns Promise resolving when update completes
*/
update(data: Partial<T>): Promise<void>;
/**
* Update specific fields using field paths
* @param field - Field path to update
* @param value - New field value
* @param moreFieldsAndValues - Additional field-value pairs
* @returns Promise resolving when update completes
*/
update(field: keyof T | FirebaseFirestoreTypes.FieldPath, value: any, ...moreFieldsAndValues: any[]): Promise<void>;
/**
* Check if two document references are equal
* @param other - Other document reference to compare
* @returns True if references are equal
*/
isEqual(other: FirebaseFirestoreTypes.DocumentReference): boolean;
}Usage Examples:
import firestore from '@react-native-firebase/firestore';
// Get document reference
const userDoc = firestore().collection('users').doc('userId');
// Alternative: direct document path
const userDoc2 = firestore().doc('users/userId');
// Get document data
const snapshot = await userDoc.get();
if (snapshot.exists) {
console.log('User data:', snapshot.data());
}
// Set document data (overwrites)
await userDoc.set({
name: 'John Doe',
email: 'john@example.com',
updatedAt: firestore.FieldValue.serverTimestamp()
});
// Update specific fields
await userDoc.update({
lastLoginAt: firestore.FieldValue.serverTimestamp(),
loginCount: firestore.FieldValue.increment(1)
});
// Update using field paths
await userDoc.update('profile.displayName', 'John D.');
// Delete document
await userDoc.delete();
// Access subcollection
const postsCollection = userDoc.collection('posts');Read document data and metadata from snapshots.
interface DocumentSnapshot<T = FirebaseFirestoreTypes.DocumentData> {
/**
* Get the document data
* @param options - Optional snapshot options
* @returns Document data or undefined if document doesn't exist
*/
data(options?: FirebaseFirestoreTypes.SnapshotOptions): T | undefined;
/**
* Get a specific field value
* @param fieldPath - Field path to retrieve
* @param options - Optional snapshot options
* @returns Field value
*/
get(fieldPath: keyof T | FirebaseFirestoreTypes.FieldPath, options?: FirebaseFirestoreTypes.SnapshotOptions): any;
readonly exists: boolean;
readonly id: string;
readonly metadata: FirebaseFirestoreTypes.SnapshotMetadata;
readonly ref: FirebaseFirestoreTypes.DocumentReference<T>;
}
interface SnapshotOptions {
serverTimestamps?: 'estimate' | 'previous' | 'none';
}Usage Examples:
import firestore from '@react-native-firebase/firestore';
const userDoc = firestore().collection('users').doc('userId');
const snapshot = await userDoc.get();
// Check if document exists
if (snapshot.exists) {
// Get all data
const userData = snapshot.data();
console.log('User:', userData);
// Get specific field
const email = snapshot.get('email');
console.log('Email:', email);
// Get nested field using FieldPath
const displayName = snapshot.get(new firestore.FieldPath('profile', 'displayName'));
// Check metadata
console.log('From cache:', snapshot.metadata.fromCache);
console.log('Has pending writes:', snapshot.metadata.hasPendingWrites);
}interface GetOptions {
/**
* Source for the get operation
* - 'default': Try cache first, then server
* - 'server': Always get from server
* - 'cache': Only get from cache
*/
source: 'default' | 'server' | 'cache';
}
interface SetOptions {
/**
* Whether to merge data with existing document
*/
merge?: boolean;
/**
* Specific fields to merge (requires merge: true)
*/
mergeFields?: (string | FirebaseFirestoreTypes.FieldPath)[];
}
interface SnapshotMetadata {
/**
* True if data came from local cache
*/
readonly fromCache: boolean;
/**
* True if document has local modifications not yet written to server
*/
readonly hasPendingWrites: boolean;
/**
* Check if metadata objects are equal
*/
isEqual(other: FirebaseFirestoreTypes.SnapshotMetadata): boolean;
}Install with Tessl CLI
npx tessl i tessl/npm-react-native-firebase--firestore