---
title: subcollection
description: >-
  Firestore pipelines subcollection() source helper: target a subcollection,
  typically paired with aggregate() and toScalarExpression().
next: /firestore/pipelines/sdk-compatibility
previous: /firestore/pipelines/timestamp-extract
---
> ## Documentation Index
> Fetch the complete documentation index at: https://rnfirebase.io/llms.txt
> Use this file to discover all available pages before exploring further.

## Prerequisites

- `@react-native-firebase/app` installed and native config completed per [Getting Started](/).
- `@react-native-firebase/firestore` installed and rebuilt into the native app.
- A Firestore **Enterprise** database. See [Pipelines overview](/firestore/pipelines).
- Import expression helpers from `@react-native-firebase/firestore/pipelines`.

`subcollection()` creates a **detached pipeline** aimed at a subcollection path relative to each parent document in the outer pipeline. The returned pipeline has no database instance and cannot be executed directly. Embed it in another pipeline with `toScalarExpression()` or `toArrayExpression()`.

# Basic example

```js
import { getFirestore } from '@react-native-firebase/firestore';
import {
  execute,
  field,
  subcollection,
  countAll,
  average,
} from '@react-native-firebase/firestore/pipelines';

const db = getFirestore('your-enterprise-database-id');

const snapshot = await execute(
  db
    .pipeline()
    .collection('restaurants')
    .addFields(
      subcollection('reviews')
        .aggregate(countAll().as('reviewCount'), average('rating').as('avgRating'))
        .toScalarExpression()
        .as('reviewSummary'),
    )
    .select('name', field('reviewSummary')),
);

snapshot.results.forEach(row => {
  const summary = row.data().reviewSummary;
  console.log(row.data().name, summary.reviewCount, summary.avgRating);
});
```

# Options object

Pass a `SubcollectionStageOptions` object when you need raw stage options:

```js
import { getFirestore } from '@react-native-firebase/firestore';
import {
  execute,
  field,
  subcollection,
  countAll,
} from '@react-native-firebase/firestore/pipelines';

const db = getFirestore('your-enterprise-database-id');

const snapshot = await execute(
  db
    .pipeline()
    .collection('restaurants')
    .addFields(
      subcollection({ path: 'reviews', rawOptions: {} })
        .aggregate(countAll().as('reviewCount'))
        .toScalarExpression()
        .as('reviewSummary'),
    )
    .select('name', field('reviewSummary')),
);

snapshot.results.forEach(row => {
  console.log(row.data().name, row.data().reviewSummary);
});
```

# Related helpers

{/* prettier-ignore */}
| Helper | Use when |
| --- | --- |
| `subcollection` | Target a subcollection relative to each parent doc |
| `toScalarExpression()` | Embed a nested pipeline as a single scalar field |
| `toArrayExpression()` | Embed a nested pipeline as an array of documents |
| `SubcollectionStageOptions` | Options type for the subcollection source stage |

# Upstream reference

See the [firebase-js-sdk `subcollection` declaration](https://firebase.google.com/docs/reference/js/firestore_pipelines#subcollection) for overload signatures and nested pipeline usage.
