0

I have following structure in firebase data

Orders -> OrderID -> LocationHistory -> LocationHistoryID -> FieldsData

Orders and LocationHistory are constant whereas OrderID ,LocationHistoryID are document ID

I want to know if it is possible to generate a query to get all LocationHistory of an order in repository which extends FirestoreReactiveRepository in rest it whould be /Orders/10002/LocationHistory/

Belwo is the code i am currently using

import org.springframework.cloud.gcp.data.firestore.Document;

import com.google.cloud.firestore.annotation.DocumentId;
import com.google.cloud.firestore.annotation.PropertyName;

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
@Document(collectionName = "Orders/10002/LocationHistory")
public class LocationHistory
{
    @DocumentId
    private String id;

    private String lat;

    @PropertyName("long")
    private String longitude;

}
Haider
  • 615
  • 1
  • 16
  • 38

1 Answers1

0

Since LocationHistory is a sub-collection of the Orders, you should retrieve the Order which will contain the LocationHistory.

@Setter
@Getter
@Document(collectionName = "Orders")
public class Order
{
    @DocumentId
    private String id;

    private List<LocationHistory> locationHistory;
}

@Setter
@Getter
public class LocationHistory
{
    @DocumentId
    private String id;

    private String lat;

    @PropertyName("long")
    private String longitude;

}
Mike E.
  • 372
  • 1
  • 6
  • please see https://stackoverflow.com/questions/60411568/mapping-firestore-collection-to-json for details i have attached images there – Haider Feb 27 '20 at 14:58
  • I added an example mapping. I hope this helps. – Mike E. Feb 27 '20 at 16:39
  • i get java.util.NoSuchElementException: No value present when querying by id – Haider Feb 27 '20 at 18:30
  • This might be because I missed `@DocumentId` on `LocationHistory`. Please try again. – Mike E. Feb 27 '20 at 21:29
  • still getting same error i created a repo https://github.com/haiderali22/spring-firebase-query-app – Haider Feb 28 '20 at 08:21
  • i tried to get collection from Firestore instance from findByIDFireStorefunction in service and its working fine – Haider Feb 28 '20 at 10:18
  • did u had a chance to see the gihub repo is there any other way to do this? – Haider Mar 02 '20 at 17:27
  • `orderRepository.findAllLocationHistoryByOrderID(id)` doesn't really make sense because it's an order repository not a `LocationHistory` repository. – Mike E. Mar 05 '20 at 18:39
  • Have you tried `orderRepository.findById(orderId)`? Then simply access the location history by calling `order.getLocationHistory()`. – Mike E. Mar 05 '20 at 18:40
  • i tried orderRepository.findById(orderId) i get java.util.NoSuchElementException: No value present – Haider Mar 06 '20 at 18:59