0

I want to get documents from a collection after a specific date time.

Example:

if the date = 30/11/2020, 12:30AM (in javascript)

  1. The query should return documents created after the above date.
  2. The query should not return documents created before the above date.

You can see my current code:

await db
    .collection(`users/${user}/messages`)
    .orderBy("cDate", "desc") /* {cDate: firestore.Timestamp.fromDate(new Date())}*/
    .limit(100)
    .get();

Any help

Muhammad
  • 2,572
  • 4
  • 24
  • 46
  • It's not really clear what you're trying to do here, since we can't see the data in your documents. But you will need to use a where clause to filter the documents in the query according to your needs. If you need help with this, you should edit the question to explain in more detail the actual contents of your documents, and what you've tried to do to filter them, as you see in the documentation: https://firebase.google.com/docs/firestore/query-data/queries#query_operators – Doug Stevenson Nov 30 '20 at 19:12
  • @DougStevenson, I think it is clear, I just want to return documents after a specific date. exampe: after 30/11/2020 12:00AM. So my query should return all docs if they are created after this date and should not return those documents after before this date – Muhammad Nov 30 '20 at 19:18
  • Do you have a date property in your documents? – Andres Gardiol Nov 30 '20 at 19:25
  • question edited please have a look at it again – Muhammad Nov 30 '20 at 19:25
  • @AndresGardiol, Yes I have it as you can see `cDate ` – Muhammad Nov 30 '20 at 19:26
  • We need to see the type of data in the documents. Are they timestamps? Integers with unix epoch offset? Strings of some sort? Without knowing specifically what's in the documents, it's not possible to build a query for it. A screenshot of actual documents to match would help. – Doug Stevenson Nov 30 '20 at 19:40
  • @DougStevenson, `{cDate: firestore.Timestamp.fromDate(new Date())}` – Muhammad Nov 30 '20 at 19:41
  • Yeah, that's information that should be in the question itself for clarity – Doug Stevenson Nov 30 '20 at 19:55

2 Answers2

2

You can do something like this:

const isoDateTime = '2020-11-30T12:30:00';
await db
    .collection(`users/${user}/messages`)
    .where('cDate', '>=', isoDate);  //<--- Its important to have an ISO formatted date string
    .orderBy("cDate", "desc")
    .limit(100)
    .get();

With this you should get all the documents after the isoDate (included). You can use > to not include the date

Andres Gardiol
  • 1,312
  • 15
  • 22
  • thank you very much, and here is `cDate` is firestore date will it works ? – Muhammad Nov 30 '20 at 19:32
  • In your firestore documents you should have an ISO DateTime string in your `cDate` properties – Andres Gardiol Nov 30 '20 at 19:34
  • thank you, I will return back after checking your answer, if it works then will be accepted – Muhammad Nov 30 '20 at 19:36
  • You can see more information about the `where` method here https://firebase.google.com/docs/firestore/query-data/queries#query_operators – Andres Gardiol Nov 30 '20 at 19:37
  • Could you please let me know why ISO date is important ? – Muhammad Nov 30 '20 at 19:40
  • Because you are comparing strings, and ISO Date format let you compare string dates with `<`,`>` and `==` operators. You can see this here: https://stackoverflow.com/questions/49327580/is-direct-comparison-of-javascript-iso-date-strings-safe – Andres Gardiol Nov 30 '20 at 19:43
  • .where("cDate", ">", new Date("11/12/2019")), is not working, Could you please help me what is the problem .... ? – Muhammad Dec 03 '20 at 20:48
  • `new Date("11/12/2019")` is an Object, you need to compare a string. You could do `new Date("11/12/2019").toISOString()`. See https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Date/toISOString to read more about that method – Andres Gardiol Dec 03 '20 at 20:50
  • I just put as an example the real code is: where("cDate", ">", new Date(new Date(). toISOString())) – Muhammad Dec 03 '20 at 21:01
  • Please once again see it here is firestore query so i can't change the `where("cDate", ..) – Muhammad Dec 03 '20 at 21:04
  • `new Date(new Date(). toISOString())` is still an object, it is not a string. `new Date()` returns a Date object. – Andres Gardiol Dec 04 '20 at 13:30
1

If you're trying to filter the documents based on a timestamp type field, you can simply provide a JavaScript date object as a filter on that field.

.where("cDate", ">", aJavaScriptDateObject)

You can also use a Firestore Timestamp object as well.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441