Python Firebase Admin Sdk 查询时间戳

Mat*_*ara 2 python google-cloud-firestore

如何检索 createAt 时间戳晚于特定日期的文档?

就像是:

firstDayOfMonth = datetime.date.today().replace(day=1)
transactions = db.collection('productTransactions').where('createdAt', ">=", firstDayOfMonth).get()
Run Code Online (Sandbox Code Playgroud)

Mic*_*kop 5

问题是你的firstDayOfMonth属于 class date,而 Firebase 使用Timestamp日期和时间的格式(在datetimePython中)。

你可以做:

firstDayOfMonth = datetime.date.today().replace(day=1)
# datetime.date(2019, 2, 1)

dt = datetime.datetime.combine(firstDayOfMonth, datetime.datetime.min.time())
# datetime.datetime(2019, 2, 1, 0, 0)

transactions = db.collection('productTransactions').where('createdAt', ">=", dt).get()

Run Code Online (Sandbox Code Playgroud)