This skill provides a complete guide for getting started with Cloud Firestore Standard Edition, including provisioning, securing, and integrating it into your application.
Provisioning
To set up Cloud Firestore in your Firebase project and local environment, use the Firebase CLI:
npx -y firebase-tools@latest firestore:databases:create --location=<location>
Initialize your project for Firestore:
npx -y firebase-tools@latest init firestore
This generates firestore.rules and firestore.indexes.json files.
Security Rules
Firestore Security Rules protect your data by controlling read and write access.
Basic security rules structure:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId} {
allow read, write: if request.auth != null && request.auth.uid == userId;
}
match /public/{document=**} {
allow read: if true;
allow write: if request.auth != null;
}
}
}
Deploy rules:
npx -y firebase-tools@latest deploy --only firestore:rules
SDK Usage (Web Modular)
import { initializeApp } from 'firebase/app';
import { getFirestore, collection, doc, getDoc, setDoc, addDoc, query, where, getDocs } from 'firebase/firestore';
const db = getFirestore(app);
// Read a document
const docRef = doc(db, 'users', userId);
const docSnap = await getDoc(docRef);
if (docSnap.exists()) {
console.log(docSnap.data());
}
// Write a document
await setDoc(doc(db, 'users', userId), { name: 'Alice' });
// Add a document (auto-ID)
const ref = await addDoc(collection(db, 'messages'), { text: 'Hello' });
// Query documents
const q = query(collection(db, 'users'), where('age', '>=', 18));
const snapshot = await getDocs(q);
snapshot.forEach(doc => console.log(doc.data()));
Indexes
Firestore requires composite indexes for queries that filter or sort on multiple fields.
- Single-field indexes: Created automatically for each field
- Composite indexes: Must be created manually for multi-field queries
- Exemptions: Can exclude fields from indexing to reduce storage costs
Deploy indexes:
npx -y firebase-tools@latest deploy --only firestore:indexes
Mirrored from https://github.com/firebase/agent-skills — original author: firebase, license: Apache-2.0. This is an unclaimed mirror. Content and ownership transfer to the author when they claim this account.