Setting Up Firestore in Native Mode: A Detailed Guide

2 minute read

What is Firestore Native Mode?

Firestore Native mode is the recommended way to use Firestore. It’s a fully managed, serverless NoSQL database that automatically scales and provides real-time updates. It’s different from Datastore mode (the older version) and offers better performance and features.

Step-by-Step Setup

1. Access the Firestore Console

  1. Go to the Google Cloud Console
  2. Select your project
  3. In the left navigation menu, find “Firestore” under “Databases”

2. Create the Database

  1. Click “Create Database”
  2. You’ll see two options:
    • Native mode (Recommended)
    • Datastore mode (Legacy)
  3. Select “Native mode”

3. Choose a Location

You’ll need to select a location for your database. This is important because:

  • It affects latency
  • It can’t be changed later
  • It should be close to your users

Common options:

  • us-central1 (Iowa) - Good default for US
  • europe-west1 (Belgium) - Good for Europe
  • asia-east1 (Taiwan) - Good for Asia

4. Choose a Starting Mode

You’ll be asked to choose a starting mode:

  1. Production mode
    • Starts with all security rules enabled
    • Requires authentication
    • Good for production applications
  2. Test mode
    • Starts with open access
    • Good for development
    • Warning: Not secure for production

For development, you can start with “Test mode” and update the security rules later.

5. Security Rules

After creation, you should set up security rules. Here’s a basic example:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    // Allow read/write access to all users under any document
    match /{document=**} {
      allow read, write: if true;  // WARNING: This is for development only!
    }
  }
}

For production, you should use more restrictive rules:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /tasks/{taskId} {
      allow read: if request.auth != null;
      allow write: if request.auth != null;
    }
  }
}

6. Verify Setup

To verify your setup, you can:

  1. Use the Firestore Console to create a test document
  2. Run a simple query using the Google Cloud Console
  3. Test with your application code

7. Cost Considerations

Firestore has a free tier that includes:

  • 50,000 reads per day
  • 20,000 writes per day
  • 20,000 deletes per day
  • 1GB of stored data

Beyond the free tier, you pay for:

  • Document reads
  • Document writes
  • Document deletes
  • Stored data
  • Network egress

8. Best Practices

  1. Indexing
    • Firestore automatically creates indexes for single-field queries
    • For compound queries, you’ll need to create composite indexes
    • You can create indexes in the Firebase Console or using the gcloud command
  2. Data Structure
    • Design your data structure based on your query patterns
    • Consider using subcollections for related data
    • Keep documents small (under 1MB)
  3. Security
    • Always use security rules
    • Never leave your database in test mode for production
    • Use authentication for all operations

9. Common Commands

# List all collections
gcloud firestore collections list

# Export data
gcloud firestore export gs://your-bucket

# Import data
gcloud firestore import gs://your-bucket

# Create an index
gcloud firestore indexes composite create --collection-group=tasks --field-config=field=title,order=ascending --field-config=field=created,order=descending

10. Monitoring

Set up monitoring in the Google Cloud Console:

  1. Go to “Monitoring”
  2. Create alerts for:
    • High read/write operations
    • Storage usage
    • Error rates

11. Backup and Recovery

Firestore automatically backs up your data, but you can also:

  1. Export data to Cloud Storage
  2. Set up scheduled exports
  3. Use the Firebase Console to restore from backups