Firebase query filter by date in Golang

Issue

This Content is from Stack Overflow. Question asked by Roy

I met a issue on query data filter by date from firestore using Golang,

    q := db.Collection("students").Where("status","==", data.Status)

    if data.FirstName != "" {
        q = q.Where("firstName", "==", data.FirstName)
    }
    if data.LastName != "" {
        q = q.Where("lastName", "==", data.LastName)
    }
    if data.PhoneNumber != "" {
        q = q.Where("phoneNumber", "==", data.PhoneNumber)
    }
    if data.Email != "" {
        q = q.Where("email", "==", data.Email)
    }
    
    // filled, updated date
    if data.UpdatedAt != "" {
        updatedAtStart, err :=  time.Parse("2006-01-02", data.UpdatedAt)
        if err != nil {
            helpers.ErrorHandler(w, http.StatusInternalServerError, "Error in parsing date: %v", err)
            return
        }

        updatedAtEnd, err :=  time.Parse("2006-01-02T15:04:05", data.UpdatedAt+"T23:59:59")
        if err != nil {
            helpers.ErrorHandler(w, http.StatusInternalServerError, "Error in parsing date: %v", err)
            return
        }
log.Printf("----------------updatedAtStart-%v, updatedAtEnd-%v", updatedAtStart, updatedAtEnd)
        q = q.Where( "updatedAt", ">=", updatedAtStart).Where("updatedAt", "<=", updatedAtEnd)
    }

    iter := q.Offset(data.PageSize * (data.PageNumber - 1)).Limit(data.PageSize).Documents(ctx)

    var list  []map[string]interface{}
    for {
        doc, err := iter.Next()
        if err == iterator.Done{
            break
        }

        if err != nil {
            return 
        }

        cardData := doc.Data()
        cardData["id"] =  doc.Ref.ID
        list = append(list, cardData)
    }
     

The updatedAt I passed into is “2022-09-13”. But nothing returns in the list. Any help?
I log the date. they looks fine.

—————-updatedAtStart-2022-09-13 00:00:00 +0000 UTC, updatedAtEnd-2022-09-13 23:59:59 +0000 UTC



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?