62818

Mongodb Aggregation Framework and timestamp

Question:

I have a collection

{ "_id" : 1325376000, "value" : 13393} { "_id" : 1325462400, "value" : 13393}

ObjectIds are Unix Timestamp and are storing as Number manually.(at insert time).

now I'm searching for a solution that i could calculate sum of values for each month with Aggregation Framework.

Answer1:

Here is a way you can do it by generating the aggregation pipeline programmatically:

numberOfMonths=24; /* number of months you want to go back from today's */ now=new Date(); year=now.getFullYear(); mo=now.getMonth(); months=[]; for (i=0;i<numberOfMonths;i++) { m1=mo-i+1; m2=m1-1; d = new Date(year,m1,1); d2=new Date(year,m2,1); from= d2.getTime()/1000; to= d.getTime()/1000; dt={from:from, to:to, month:d2}; months.push(dt); } prev="$nothing"; cond={}; months.forEach(function(m) { cond={$cond: [{$and :[ {$gte:["$_id",m.from]}, {$lt:["$_id",m.to]} ]}, m.month, prev]}; prev=cond; } ); /* now you can use "cond" variable in your pipeline to generate month */ db.collection.aggregate( { $project: { month: cond , value:1 } }, { $group: {_id:"$month", sum:{$sum:"$value"} } } )

Recommend

  • Reset and keep the dates as default dates in datepicker
  • Kendo Date Picker Max Date
  • How to convert rows into columns in SQL Server?
  • Complex Maven2 with Flex4 Setup
  • Group By and add columns
  • how to programmatically acces the window and mac trusted certificate store
  • UWP : Print with out Printer Popup Dialoag
  • Filtering SPARQL results by day and month
  • How to get the position of a click event relative to its layer in KineticJS?
  • How to get or calculate size of Azure File/Share or Service
  • How to get list of users who's birthday is today in MongoDB
  • How to model a mixture of finite components from different parametric families with JAGS?
  • How to use arithmetic operators with SAS macro variables [duplicate]
  • Is it possible to send skype chat messages from a linux server without X?
  • SQL Server re-calculate or not?
  • How to concat Pandas dataframe columns
  • Receive mouse move even cursor is outside control
  • ConnectivityManager.CONNECTIVITY_ACTION deprecated
  • android Navigation Bar hiding and persantage of usable screen overlap
  • SQL - Select lowest values with group by and order by?
  • jquery validation - waiting for remote check to complete
  • Find Previous month name using Calender or any classes that in java
  • R convert summary result (statistics with all dataframe columns) into dataframe
  • Approximate Order-Preserving Huffman Code
  • Invalid Date on validation Date of js
  • how does System.Web.HttpRequest::PathInfo work?
  • Scala multiline string placeholder
  • Does Mobilefirst provide a provision to access web services directly?
  • Not able to aggregate on nested fields in elasticsearch
  • Grails calculated field in SQL
  • Get history of file changes from TFS to implement custom “blame”-behaviour of exceptions
  • Custom Tabgroup Appcelerator
  • Exchange data b/w iOS devices using Bluetooth 4.0
  • Is there a amazon webstore API for customers?
  • Is possible to count alias result on mysql
  • Join two tables and save into third-sql
  • Convert array of 8 bytes to signed long in C++
  • Understanding cpu registers
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices