3

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
  • 12,319
  • 5
  • 67
  • 77
K20GH
  • 6,032
  • 20
  • 78
  • 118
  • 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

11
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,995
  • 10
  • 16
  • Ah yeah that makes sense thanks. Just realised it'd be a `<` operator rather than `>` though :) – K20GH Sep 02 '19 at 09:53