Firestore query by date range

前端 未结 9 914
悲&欢浪女
悲&欢浪女 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:27

    Those who, like me, are using PHP to access Firestore, can do something like this:

    $startTime = new DateTime('2020-05-23 00:00:00');
    $endTime = new DateTime('2020-06-23 23:59:59');
    
    $start = new Google\Cloud\Core\Timestamp($startTime);
    $end = new Google\Cloud\Core\Timestamp($endTime);
    
    // fb is a Google\Cloud\Firestore\FirestoreClient object
    $this->query = $this->fb->collection('your_collection');
    
    $aux = $this->query;
    $aux = $aux->where('startTime', '<', $end);
    $aux = $aux->where('startTime', '>', $start);
    
    return $aux->documents();
    

    Enjoy.

提交回复
热议问题