GoogleCloudPlatform / GoogleCloudPlatform/spring-cloud-gcp

Add Support for Fetching Documents from Subcollections in FirestoreReactiveRepository

Open
#1,574 1 comment 7 reactions 0 assignees View on GitHub
firestore priority: p3 type: enhancement
Dominant language
Java
Stars
551
Forks
349
Avg merge
1d 13h
Merged PRs (30d)
14

Description

The FirestoreReactiveRepository currently supports fetching embedded collections, but does not support fetching subcollections. This requires two separate calls to Firestore and is not optimal.

It would be ideal if the FirestoreReactiveRepository could fetch documents from subcollections through a findById method or another method that brings the subcollections with it. This would allow for a single call to Firestore to fetch both the parent document and its subcollections.

Example code for fetching subcollections currently:

```
var collectionContent = repository.findById(collectionId);

collectionContent.setSubCollectionContent(
firestoreTemplate
.withParent(collectionId, CollectionClass.class)
.findAll(SubCollectionClass.class)
.collectList()
.block()
);
```

So you can see that currently it is necessary to make 2 different calls for something that could be one. Because if you make only:

```
var collectionContent = repository.findById(collectionId);
```

The subcollection of collectionContent will be null.

It'd be awesome if we could do it in one call :).

In these links you can see more users frustrated by the lack of this feature:
https://stackoverflow.com/questions/62043573/how-to-fetch-documents-from-subcollection-in-firestore-using-spring-data
https://stackoverflow.com/questions/61839619/how-can-i-retrieve-a-subcollection-in-firestore-with-spring-framework

Contributor guide

Open the contributing guide

Research direction

Start by locating FirestoreReactiveRepository and tracing findById alongside the existing embedded-collection support. Review the Firestore and Spring Data APIs involved before defining whether a repository method can return the parent and subcollections together; done should be demonstrated by tests showing both are populated without the current separate call.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend, database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.