2

I have a field in my document that is stored as a Timestamp like so

  "created_at": admin.firestore.FieldValue.serverTimestamp(),

What I want to do is find all documents using the admin lib in Cloud Functions, that have this field over 24 hours old

const tsToMillis = admin.firestore.Timestamp.now().toMillis();
const results = await files.where("created_at", ">", tsToMillis - (24 * 60 * 60 * 1000)).get();

I have tried the above, however I'm not sure how to get the value to compare it, or is there a way to do this directly with the Timestamp in FS without converting it?

Gastón Saillén
  • 10,654
  • 5
  • 58
  • 68
K20GH
  • 5,643
  • 18
  • 68
  • 110
  • I believe the best way is to actually create a new date object and use that in the query. https://stackoverflow.com/questions/47000854/firestore-query-by-date-range – chrismclarke Sep 02 '19 at 09:46

1 Answers1

9
const tsToMillis = admin.firestore.Timestamp.now().toMillis();
const compareDate = new Date(tsToMillis - (24 * 60 * 60 * 1000))
const results = await files.where("created_at", "<", compareDate ).get();
chrismclarke
  • 1,842
  • 8
  • 14