How to get previous document of last 30 (custom) days using mongodb query

Thanks @Takis for your efforts, If you closely look then you will see that accountID = 01 have 5 records in last 30 days.


ISODate(β€œ2020-12-23T00:00:00Z”) = newest
ISODate(β€œ2020-12-22T00:00:00Z”) 1 days before
ISODate(β€œ2020-12-21T00:00:00Z”) 2 days
ISODate(β€œ2020-12-20T00:00:00Z”) 3 days
ISODate(β€œ2020-12-19T00:00:00Z”) 4 days
ISODate(β€œ2020-12-18T00:00:00Z”) 5 days
ISODate(β€œ2020-12-17T00:00:00Z”) 6 days
ISODate(β€œ2020-12-16T00:00:00Z”) 7 days

All are <30 days from the newest so all pass,count=8


ISODate(β€œ2020-11-19T00:00:00Z”) =newest
ISODate(β€œ2020-11-18T00:00:00Z”) 1 day before
ISODate(β€œ2020-11-10T00:00:00Z”) 9 days before
ISODate(β€œ2020-11-02T00:00:00Z”) 17 days before
ISODate(β€œ2020-07-19T00:00:00Z”) 4 months before

The first 4 are <30 days from the newest and pass ,count=4
The last one is rejected

The last query works this way,its possible that i dont know what you wanted
but why you say acount=01 has 5 ,can you give the dates like above,and explain it?

oh, my bad.
Thanks @Takis for correcting me.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.