Firestore query by date range

前端 未结 9 916
悲&欢浪女
悲&欢浪女 2020-12-02 12:11

I need the help to query long collection with date range. See the below example document. I wanna query startTime field using date range.

9条回答
  •  隐瞒了意图╮
    2020-12-02 12:35

    Generic function to find documents in a collection by date range of specifics fields:

    public List findDocsByDateRange(String collection, 
                                                           String fieldStartDate,
                                                           String fieldEndDate,
                                                           Date startDate, 
                                                           Date endDate) {
        ApiFuture querySnapshot = fireStore()
                        .collection(collection)
                        .whereGreaterThanOrEqualTo(FieldPath.of(fieldStartDate), startDate)
                        .whereLessThanOrEqualTo(FieldPath.of(fieldEndDate), endDate)
                        .get();
        return querySnapshot.get().getDocuments();
    }
    

    Packages:

    import com.google.api.core.ApiFuture;
    import com.google.cloud.firestore.DocumentSnapshot;
    import com.google.cloud.firestore.FieldPath;
    import com.google.cloud.firestore.Firestore;
    import com.google.cloud.firestore.QueryDocumentSnapshot;
    import com.google.cloud.firestore.QuerySnapshot;
    

提交回复
热议问题