Matias Vergara
Matias Vergara

Reputation: 99

Python Firebase Admin Sdk Query Timestamp

How can I retrieve documents that have a createdAt timestamp newer than a certain date?

Something like:

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

Upvotes: 2

Views: 2463

Answers (1)

Michal Skop
Michal Skop

Reputation: 1399

The problem is that your firstDayOfMonth is of class date, while the Firebase uses format Timestamp for dates and time (which is datetime in Python).

You can do:

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()

Upvotes: 6

Related Questions