查询Firestore数据库的日期范围

时间:2020-03-27 19:03:21

标签: swift firebase google-cloud-firestore timestamp

我正在构建一个应用程序,以帮助人们从机场拼车,并且我正在尝试查询数据库中特定范围内的游乐设施。

这就是我所拥有的:

import UIKit
import Firebase

class DiscoverRideViewController: UIViewController {

    var userFullName = String()
    var dateEarly = Constants.UserInfo.chosenDate.addingTimeInterval(-900)
    var offset = TimeInterval()

    @IBOutlet weak var createRide: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        print(dateEarly)
        offset = TimeInterval(TimeZone.current.secondsFromGMT())
        print("timezone offset: ", offset)
        dateEarly = dateEarly.addingTimeInterval(offset)

        print("searching for documents:")
        let db = Firestore.firestore()
        db.collection("rides-wfu").whereField("aiport", isEqualTo: Constants.UserInfo.chosenAirport).whereField("ridetime", isGreaterThanOrEqualTo: dateEarly).getDocuments() { (querySnapshot, err) in
            if let err = err {
                print("Error getting documents: \(err)")
            } else {
                for document in querySnapshot!.documents {
                    print("\(document.documentID) => \(document.data())")
                }
            }
        }
    }

    @IBAction func newRideTapped(_ sender: Any) {
        let db = Firestore.firestore()
        print(Constants.UserInfo.chosenDate)
        db.collection("rides-wfu").addDocument(data: ["airport":Constants.UserInfo.chosenAirport, "ridetime":Constants.UserInfo.chosenDate, "groupmembers":[Constants.UserInfo.firstName+" "+Constants.UserInfo.lastName, "", "", ""]])
    }
}

创建旅程功能成功,但是查询时遇到问题,是的,我已经建立了索引。我已经尝试了几乎所有内容,并且db.collection()。where()方法由于某种原因在编译器上创建了错误-仅whereField()方法有效。任何帮助或指导将不胜感激。

Constants class: 我的常量类似乎在起作用,这是我在视图控制器之间共享变量的方式。

This is the error I receive when I try to use 'where' instead of 'whereField', although from the documentation it looks like 'where' is a valid command for querying the Firestore database. 可能我需要下载其他任何Pod吗?我有的是: pod“ Firebase / Analytics” pod“ Firebase / Auth” pod“ Firebase / Core” pod“ Firebase / Firestore”

1 个答案:

答案 0 :(得分:1)

您现在将其作为查询:

db.collection("rides-wfu").where("ridetime", ">", timestampEarly)

这不是Swift的有效查询语法。您正在寻找的是:

db.collection("rides-wfu").whereField("ridetime", isGreaterThanOrEqualTo: timestampEarly)

我强烈建议您研究querying Firestore上的Firebase文档,因为其中包含该示例以及更多类型的查询。