Aggregate, group by week, cross year

Hi.
I’m developing some software using mongodb and I happened to need a way for aggregate the data by week.
But I don’t know how to do it when the week is crossing year.
I’m now doing it by using

$dateToString{
  format : "_%Y_ _ _ _ _ _%U_",
  date : $foundTime
}

and then group by this string.

when the weeks are inside of one year, everything goes fine.

but when things go to the week accrossing two years, it will be seperate into two segments,

one be “2020 _ _ _ _ 52”, another be “2021 _ _ _ _ 00

I have no idea how to merge them.

Help please…

your problem is solved if you use ISO week(%V) rather then normal week(%Y) i.e : $dateToString{
format : “%Y _ _ _ _ %V”,
date : $foundTime
}:

%V Week of Year in ISO 8601 format

New in version 3.4 . 01 - 53

more web references…

1 Like