3

How to fetch documents from subcollection using spring FirestoreReactiveRepository?

Ask is: how subcollection name or path can be given dynamically to @Document annotation?

My firestore collection: (cities is first level collection and apartments is subcollection of this)

cities - nyc (id)- |- name: New York City
                   |- country: USA
                   └ apartments - part_avenue (document id)
                                ├ name: Park Avenue
                                └ zip: 10022
                                - plaza (document id)
                                ├ name: Plaza Riverhouse 
                                └ zip: 10132

City POJO is

@Document(collectionName = "cities")
public class City {

  @DocumentId
  private String id;
  private String name;
  private String country;
  private List<Apartment> apartments;
}

Apartment POJO is:

@Document(collectionName = "apartments")
public class Apartment {

  @DocumentId
  private String id;
  private String name;
  private Integer zip;
}

CityRepository which fetches data from cities collection

public interface CityRepository extends FirestoreReactiveRepository<City> {

}

On calling findById(nyc) returns apartments as null. I want to populate subcollection as well in city object.

Hi Dmirty,

In the given example pets are stored as part of user object(collection) but I want to store pets in a separate sub collection under users collection.

Result of given example: enter image description here

Actual ask is pets should be subcollection like below: So, it would be helpful if you can provide an example to store pets as subcollection and then how to retrieve it.

enter image description here

piyush garg
  • 71
  • 1
  • 6

1 Answers1

0

That should work. Take a look at this sample - it does something similar. You can run it and see if it works for you (it definitely works for me).

One potential issue is - are you missing public setters in your City class? They are required in order for conversion to work properly.

If that doesn't help, could you create a minimal application that replicates the issue (maybe on github) so I can take a closer look?

Dmitry S
  • 21
  • 3
  • In the given example pets is part of user collection and in my case pets is subcollection of users collection. I have provided screen shot in below comment. – piyush garg Jun 02 '20 at 18:55