Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8649385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:32:56+00:00 2026-06-12T13:32:56+00:00

I’m storing minutely performance data in MongoDB, each collection is a type of performance

  • 0

I’m storing minutely performance data in MongoDB, each collection is a type of performance report, and each document is the measurement at that point in time for the port on the array:

{
  "DateTime" : ISODate("2012-09-28T15:51:03.671Z"),
  "array_serial" : "12345",
  "Port Name" : "CL1-A",
  "metric" : 104.2
}

There can be up to 128 different “Port Name” entries per “array_serial”.

As the data ages I’d like to be able to average it out over increasing time spans:

  • Up to 1 Week : minute
  • 1 Week to 1 month : 5 minute
  • 1 – 3 months: 15 minute

etc..
Here’s how I’m averaging the times so that they can be reduced :

var resolution = 5; // How many minutes to average over     
var map = function(){
        var coeff = 1000 * 60 * resolution;
        var roundTime = new Date(Math.round(this.DateTime.getTime() / coeff) * coeff);
        emit(roundTime, { value : this.metric, count: 1 } );
 };

I’ll be summing the values and counts in the reduce function, and getting the average in the finalize funciton.

As you can see this would average the data for just the time leaving out the “Port Name” value, and I need to average the values over time for each “Port Name” on each “array_serial”.

So how can I include the port name in the above map function? Should the key for the emit be a compound “array_serial,PortName,DateTime” value that I split later? Or should I use the query function to query for each distinct serial, port and time? Am I storing this data in the database correctly?

Also, as far as I know this data gets saved out to it’s own collection, what’s the standard practice for replacing the data in the collection with this averaged data?


Is this what you mean Asya? Because it’s not grouping the documents rounded to the lower 5 minute (btw, I changed ‘DateTime’ to ‘datetime’):

    $project: {
                "year" : { $year : "$datetime" },
                "month" : { $month : "$datetime" },
                "day" : { $dayOfMonth : "$datetime" },
                "hour" : { $hour : "$datetime" },
                "minute" : { $mod : [ {$minute : "$datetime"}, 5] },
                array_serial: 1,
                port_name: 1,
                port_number: 2,
                metric: 1
}

From what I can tell the “$mod” operator will return the remainder of the minute divided by five, correct?

This would really help me if I could get the aggregation framework to do this operation rather than mapreduce.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:32:57+00:00Added an answer on June 12, 2026 at 1:32 pm

    Here is how you could do it in aggregation framework. I’m using a small simplification – I’m only grouping on Year, Month and Date – in your case you will need to add hour and minute for the finer grained calculations. You also have a choice about whether to do weighted average if the point distribution is not uniform in the data sample you get.

    project={"$project" : {
            "year" : {
                "$year" : "$DateTime"
            },
            "month" : {
                "$month" : "$DateTime"
            },
            "day" : {
                "$dayOfWeek" : "$DateTime"
            },
            "array_serial" : 1,
            "Port Name" : 1,
            "metric" : 1
        }
    };
    group={"$group" : {
            "_id" : {
                "a" : "$array_serial",
                "P" : "$Port Name",
                "y" : "$year",
                "m" : "$month",
                        "d" : "$day"
            },
            "avgMetric" : {
                "$avg" : "$metric"
            }
        }
    };
    
    db.metrics.aggregate([project, group]).result
    

    I ran this with some random sample data and got something of this format:

    [
        {
            "_id" : {
                "a" : "12345",
                "P" : "CL1-B",
                "y" : 2012,
                "m" : 9,
                "d" : 6
            },
            "avgMetric" : 100.8
        },
        {
            "_id" : {
                "a" : "12345",
                "P" : "CL1-B",
                "y" : 2012,
                "m" : 9,
                "d" : 7
            },
            "avgMetric" : 98
        },
        {
            "_id" : {
                "a" : "12345",
                "P" : "CL1-A",
                "y" : 2012,
                "m" : 9,
                "d" : 6
            },
            "avgMetric" : 105
        }
    ]
    

    As you can see this is one result per array_serial, port name, year/month/date combination. You can use $sort to get them into the order you want to process them from there.

    Here is how you would extend the project step to include hour and minute while rounding minutes to average over every five minutes:

    {
        "$project" : {
            "year" : {
                "$year" : "$DateTime"
            },
            "month" : {
                "$month" : "$DateTime"
            },
            "day" : {
                "$dayOfWeek" : "$DateTime"
            },
            "hour" : {
                "$hour" : "$DateTime"
            },
            "fmin" : {
                "$subtract" : [
                    {
                        "$minute" : "$DateTime"
                    },
                    {
                        "$mod" : [
                            {
                                "$minute" : "$DateTime"
                            },
                            5
                        ]
                    }
                ]
            },
            "array_serial" : 1,
            "Port Name" : 1,
            "metric" : 1
        }
    }
    

    Hope you will be able to extend that to your specific data and requirements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.