Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
MongoDB aggregate count the number of products between hour range

I'm using mongodb charts. is it possible to get the number of products within the time range of (eg: 12:00am - 5:00 AM)

For example I have 2 products added at 3:00AM and then I will have 5 products added at 4:00AM.

I'm counting the products by it's createdAt field.

I have a sample data like this:

Products: {
        "_id": {
            "$oid": "603c62272aacbc0017e2e4b5"
        },
        "amount": 110,
        "user": {
            "$oid": "5f7408cf7889580017c369a1"
        },
        "transaction_id": "cZ3fgffFI",
        "createdAt": {
            "$date": "2021-03-01T03:40:23.300Z"
        },
        "updatedAt": {
            "$date": "2021-03-01T04:13:25.908Z"
        },
        "__v": 0,
    }, 
{
            "_id": {
                "$oid": "603c62272aacbc0017e2e4b5"
            },
            "amount": 110,
            "user": {
                "$oid": "5f7408cf7889580017c369a1"
            },
            "transaction_id": "cZ3fgffFI",
            "createdAt": {
                "$date": "2021-03-01T03:40:23.300Z"
            },
            "updatedAt": {
                "$date": "2021-03-01T04:13:25.908Z"
            },
            "__v": 0,
        },
{
            "_id": {
                "$oid": "603c62272aacbc0017e2e4b5"
            },
            "amount": 110,
            "user": {
                "$oid": "5f7408cf7889580017c369a1"
            },
            "transaction_id": "cZ3fgffFI",
            "createdAt": {
                "$date": "2021-03-01T03:40:23.300Z"
            },
            "updatedAt": {
                "$date": "2021-03-01T04:13:25.908Z"
            },
            "__v": 0,
        }

Output:

I'm counting the products using the createdAt field.

3:00 AM 2 products 4:00 AM 5 products

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can simply use group by hours to get count per hours data like below,

[{
    '$match':{
       '$and':[ {'createdAt':{'$gte':'2021-03-08T00:00:00.000Z'}},{'createdAt':{'$lte':'2021-03-08T05:00:00.000Z'}}]
    }
},
    {
    '$project': {
      'hours': {
        '$hours': 'createdAt'
      }
    }
  }, {
    '$group': {
      '_id': {
        'hours': '$hours', 
      }, 
      'total': {
        '$sum': 1
      }, 
      'hours': {
        '$hours': '$hours'
      }
    }
  }
]

also, you can add any other condition using match for other filters that you need.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!