score:2
you seem to just want the timestamp values returned from the result. there is indeed a simple way to do this in the aggregation framework without using the date aggregation operators. you can use basic "date math" instead, and with a trick that can be used to extract the "timestamp" value from the date object and manipulate it:
db.collection.aggregate([
{ "$group": {
"_id": {
"$subtract": [
{ "$subtract": [ "$time", new date("1970-01-01") ] },
{ "$mod": [
{ "$subtract": [ "$time", new date("1970-01-01") ] },
1000 * 60 * 60 * 24
]}
]
},
"avg": { "$avg": "$temp_c" }
}}
])
so the basic "trick" there is that when subtract one date object from another ( or similar operation ) the result returned is a number for "milliseconds" of the time difference between the two. so by using the "epoch" date of "1970-01-01", you get the "epoch timestamp" value for the date object as a number.
then the basic date math is applied by subtracting from this value the modulo ( or remainder ) from the milliseconds in a day. this "rounds" the value to represent the "day" on which the entry is recorded.
i like posting the json because it parses everywhere, but in a more php way, then like this:
$collection->aggregate(array(
array( '$group' => array(
'_id' => array(
'$subtract' => array(
array( '$subtract' => array(
'$time', new mongodate(strtotime("1970-01-01 00:00:00"))
) ),
array( '$mod' => array(
array( '$subtract' => array(
'$time', new mongodate(strtotime("1970-01-01 00:00:00"))
) ),
1000 * 60 * 60 * 24
))
)
),
"avg" => array( '$avg' => '$temp_c' )
))
))
so that is a little cleaner than using the date aggregation operators to get to your intended result. of course this is still not "all the way" to how you want the data to be presented where you can use it in the client.
the real thing to do here is manipulate the result so that you get the output format you want. this is probably better suited to your server code doing the manipulation before you return the response, but if you have mongodb 2.6 or greater then it is "possible" to do this within the aggregation pipeline itself:
db.collection.aggregate([
{ "$group": {
"_id": {
"$subtract": [
{ "$subtract": [ "$time", new date("1970-01-01") ] },
{ "$mod": [
{ "$subtract": [ "$time", new date("1970-01-01") ] },
1000 * 60 * 60 * 24
]}
]
},
"avg": { "$avg": "$temp_c" }
}},
{ "$group": {
"_id": null,
"data": {
"$push": {
"$map": {
"input": { "$literal": [ 1,2 ] },
"as": "el",
"in": {
"$cond": [
{ "$eq": [ "$$el", 1 ] },
"$$_id",
"$avg"
]
}
}
}
}
}}
])
so this is pretty sneaky really. after the initial "grouping" is done to determine the averages for each day you get two fields in your result documents per day for _id
and avg
. what the $map
operator does here is takes and array as input ( in this case, just a numbered template with a pair of values to identify position ) and processes each element to return an array equal to the elements present in the original.
the $cond
operator here allows you to look at the value of the current element of that array and "swap it" with another value present in the current document. so for each document, the results contain something that is a paired array like:
[ 1409346800000, 12 ]
then all that happens is all results are pushed into a single document with a "data" array that appears as follows:
{ "_id": null, "data": [ [..,..], [..,..], (...) ] }
now your data element in that one result is an array of array pairs representing the points you want.
of course though, operators like $map
are only available from mongodb 2.6 and onwards, so if you have that available then you can use them but otherwise just process the results in code with a similar "map" operation:
function my_combine($v) {
return array($v["_id"],$v["avg"])
}
$newresult = array_map( "my_combine", $result )
so this really comes down to array manipulation from whichever way you approach it, but the date manipulation trick should also save you some work in obtaining the results as the expected timestamp values as well.
Source: stackoverflow.com
Related Query
- Issue with Dates - trying to plot MongoDB data in Highcharts via PHP
- Highcharts data series issue with ajax/json and PHP
- Dates not displaying correctly in Highcharts plot with irregular time data
- Highcharts cloud issue with data source when duplicating chart
- Highcharts highstock plot with data from php
- Reload chart data via JSON with Highcharts
- Highcharts scatter plot with lots of data points running really slow
- Highcharts - Global configuration with common code and unique data & Headings
- How to populate a Highcharts axis with string formatted data from a PHP array
- How to input plot data into highcharts with rails
- JSON Data Map Issue with HighCharts + Ajax
- Issue with JSON data encoded from the server, not able to plot the chart
- Generating Highcharts from MySQL data with PHP does not work
- Plot Highcharts data from past 30 days with Ruby on Rails
- Have an issue with JavaScript, AJAX code displaying data
- Data from MongoDB results with empty Highcharts graph
- Importing Data in Highcharts with PHP
- Issue with json data mapping with Highcharts
- Highcharts with data populated by php generated html table - only one will load
- Highcharts series visibility with csv data source
- Highcharts column chart with data from mongodb
- Highcharts Pie chart data Format via php
- Populating highcharts with php via json_encode
- Invalid dates in Highcharts with data loaded by JSON
- Highcharts - Issue with showing and hiding plot lines and bands on legendItemClick
- How to have multiple highcharts with different series data in vuejs without repeating code
- Highcharts - Issue with js counter for data display
- Highcharts Pyramid Chart displays no data with JSON, PHP
- Dynamic column based highcharts feeding data with jquery and php
- how do you create custom buttons with highcharts that each data buttons calls anather php script for its data
More Query from same tag
- Highcharts wind direction
- Highcharts - Same chart multiple times in same page
- highstock issue : pointInterval property is not working when series data set contains date and value?
- Handling two ajax calls on single page to pull data into Highcharts on another page
- Passing series data from Java to Multi Y axis Highcharts
- highcharts configuration
- Highcharts: how to highlight yAxis value?
- highcharts line for one point serie
- highcharts options clickable category name
- Modify the color of series in highcharts-ng
- Dotnet highcharts not working (Missing assembly)
- How to get different colors for every column in a stacked column chart
- Multiple axis does not work on Highcharts 4
- highstock chart only set the max value of xAxis
- highcharts - drilldown: does not workn with AJAX
- Highcharts will not display data
- highchart highstock not display full yAxis data
- Customizing the Stockchart range selector buttons from HighCharts library in GWT (and Javascript in general)
- Pie chart from highcharts does not render via justpy
- Highcharts full circle gauge as in Knob js
- Highcharts, vue component. 3 charts in 1
- Highcharts polar chart - zero at centre
- How to add new symbols in Highstock chart?
- Using LINQ to generate Highchart column table data
- How to differentiate the xAxis and yAxis plotbands in Highchart
- Highcharts synchronized charts combining with other charts
- Reports with parameter working fine independently but not functional in cockpit (composite document)
- Change Highchart Options dynamically in angular
- HighStocks with Reactjs - Make it occupy full size of div container
- Hiding HighCharts StackLabels when series is large