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

8
  • 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: firebase.google.com/docs/firestore/query-data/… Commented Nov 30, 2020 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 Commented Nov 30, 2020 at 19:18
  • Do you have a date property in your documents? Commented Nov 30, 2020 at 19:25
  • question edited please have a look at it again Commented Nov 30, 2020 at 19:25
  • @AndresGardiol, Yes I have it as you can see cDate Commented Nov 30, 2020 at 19:26

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

11 Comments

thank you very much, and here is cDate is firestore date will it works ?
In your firestore documents you should have an ISO DateTime string in your cDate properties
thank you, I will return back after checking your answer, if it works then will be accepted
You can see more information about the where method here firebase.google.com/docs/firestore/query-data/…
Could you please let me know why ISO date is important ?
|
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.

8 Comments

will it be supported by any date object ?
example: new Data().toISOString()
No, that's a string, not a date object.
Is that possible to save in firestore as {cDate: new Date()}. if yes will still orderBy works ?
That also saves a timestamp that can be ordered with other timestamps.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.