iosswiftgoogle-cloud-firestoregeofiregeofirestore

Not able to query documents by geohash in google firestore, using created date field?


I am using Google Firestore geoqueries Following this documentation. Querying documents within a distance using geohash works fine. When I introduce a new condition: `.whereField("createdOn", isGreaterThan: <value for time interval since 1970 7 days ago>)

This throws an error saying, any inequality on a field must have this field as the first 'OrderBy' parameter. When I add order by parameter for this field, it no longer returns the document that is still within the distance searched but shows no error.

Is it even possible to use the firestore geoqueries with additional query conditions?

I need to be able to limit the query by objects created within a certain timeframe, otherwise this will return a very large number of documents. Sorting these post-query will surely impact app performance. Maybe I am missing a more practical way of using geoqueries in Firestore?

 let queryBounds = GFUtils.queryBounds(forLocation: center,
                                              withRadius: distanceM)
    
    //test
    let ref = Ref().databaseJobs
    let currentTime = NSDate().timeIntervalSince1970
    let intervalLastWeek = currentTime - (10080 * 60)
    print("current time is: \(currentTime) and 7 days ago interval was \(intervalLastWeek)")
    ref.whereField("createdOn", isGreaterThan: intervalLastWeek)
    
    
    let queries = queryBounds.compactMap { (any) -> Query? in
        guard let bound = any as? GFGeoQueryBounds else { return nil }
        return ref
            .order(by: "geohash")
            .start(at: [bound.startValue])
            .end(at: [bound.endValue])
            .whereField("createdOn", isGreaterThan: intervalLastWeek)

Solution

  • Firestore can only filter on range on a single field. Or simpler: you can only have a single orderBy clause in your query.

    What you are trying to do requires two orderBy clauses, one for geohash and one for createdOn, which isn't possible. If you were to need an equality check on a second field though, that would be possible as thstt doesn't require an orderBy clause.

    What I'm wondering is whether you can add a field createdOnDay that contains just the day part of createdOn in a fixed format, and then perform an in on that with 7 values (for the days of the past week?