DEV Community

Cover image for MongoDB Marvels - Slicing Data by Dates - Series #14
Functional Javascript
Functional Javascript

Posted on • Edited on

MongoDB Marvels - Slicing Data by Dates - Series #14

Intro

Let's look at a simple query first as we prep ourselves for more thorough queries in this Series:

Query for how many user actions per hour block.
This will report which hour blocks the site is busiest.

  mgArr(dbEnum.nlpdb, collEnum.users_actions,
    {
      $addFields: {
        hourUtc: {
          $hour: { date: "$_id" }
        },
        hourPst: {
          $hour: { date: "$_id", timezone: "-08:00" },
        },
      },
    },
    groupByKey("hourPst"),
    sortDesc("count"),
  )
Enter fullscreen mode Exit fullscreen mode

Notes

The server timestamp is in UTC Time (Coordinated Universal Time).

The db query code also converts the timezone from UTC to PST.
(In another post we'll extract the user's timezone from their device.)

The query has 3 stages:

The $addFields stage adds two computed fields to our resultset, "hourUtc" and "hourPst". (Just to show an example of how to get dates without and with an offset)

The Group stage will pivot on the hour component of the date, for the PST timezone.

Sort Descending, so we see the busiest hours at the top.

We will get back 24 records (24 hours in each day), so we don't need to Paginate.

The output:

/*
 { count: 610, hourPst: 15 },
  { count: 58, hourPst: 20 },
  { count: 55, hourPst: 21 },
  { count: 51, hourPst: 23 },
  { count: 49, hourPst: 14 },
  { count: 41, hourPst: 10 },
  { count: 38, hourPst: 13 },
  { count: 34, hourPst: 22 },
  { count: 33, hourPst: 18 },
  { count: 32, hourPst: 16 },
  { count: 27, hourPst: 17 },
  { count: 24, hourPst: 11 },
  { count: 23, hourPst: 0 },
  { count: 17, hourPst: 19 },
  { count: 17, hourPst: 1 },
  { count: 15, hourPst: 12 },
  { count: 9, hourPst: 3 },
  { count: 5, hourPst: 2 },
  { count: 5, hourPst: 8 },
  { count: 3, hourPst: 6 }
*/
Enter fullscreen mode Exit fullscreen mode

Notes

Looks like 3:00:00 to 3:59:59 PM is very busy for some reason. :)

Though this is an example query, you can see how you can get meaningful and sometimes surprising insights into your data by carving it up into timeseries dimensions.

What's Next

We'll cover more power with dates and time series in this Series of Articles.

As always, if you have an questions or input, let me know.

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Tiger Data image

🐯 🚀 Timescale is now TigerData: Building the Modern PostgreSQL for the Analytical and Agentic Era

We’ve quietly evolved from a time-series database into the modern PostgreSQL for today’s and tomorrow’s computing, built for performance, scale, and the agentic future.

So we’re changing our name: from Timescale to TigerData. Not to change who we are, but to reflect who we’ve become. TigerData is bold, fast, and built to power the next era of software.

Read more

👋 Kindness is contagious

Dive into this thoughtful piece, beloved in the supportive DEV Community. Coders of every background are invited to share and elevate our collective know-how.

A sincere "thank you" can brighten someone's day—leave your appreciation below!

On DEV, sharing knowledge smooths our journey and tightens our community bonds. Enjoyed this? A quick thank you to the author is hugely appreciated.

Okay