0

I have this code in Javascript to get data from my MongoDB at an interval of 15mins and store in JSON. I'm not getting any errors but I'm sure I'm doing something wrong.:

test.js

var datetime = new Date();
var y = new Date();
datetime.setHours(datetime.getHours() - 168);
y.setMinutes(15);

 // construct aggregation pipeline retrieving last 15 minutes
var pipeline =  [
    {
        $group: {
            _id: {
                Time: "$datetime",
                Humidity: { $avg: "$humidity" }
            },
        },
    },
    { 
        $project : { 
            _id : 0, 
            "Time": {
                $range: [0, "$datetime", y]
            }, 
            Humidity: 1 
        } 
    },
    { $sort: { "_id.Time": -1, "Humidity": -1 } }
];

I will appreciate any efforts.

These are the items in my collection

[{ "_id" : ObjectId("585db63541472bf7683eb3c5"), "datetime" : ISODate("2016-12-23T23:41:41.897Z"), "temperature" : 19, "humidity" : 23 }
{ "_id" : ObjectId("585db63a41472bf7683eb3c6"), "datetime" : ISODate("2016-12-23T23:41:46.927Z"), "temperature" : 19, "humidity" : 23 }
{ "_id" : ObjectId("585db63f41472bf7683eb3c7"), "datetime" : ISODate("2016-12-23T23:41:51.956Z"), "temperature" : 19, "humidity" : 23 }
{ "_id" : ObjectId("585db644483ab4f778a635bf"), "datetime" : ISODate("2016-12-23T23:41:56.991Z"), "temperature" : 19, "humidity" : 23 }
{ "_id" : ObjectId("585db64a483ab4f778a635c0"), "datetime" : ISODate("2016-12-23T23:42:02.032Z"), "temperature" : 19, "humidity" : 23 }]

I would like to like to get a JSON data that outputs the Average humidity with an interval of 15 mins over a 1 hour period with a specific start time and end time.

[{ "_id" : ObjectId("585db63541472bf7683eb3c5"),
 "datetime" : ISODate("2016-12-23T23:41:41.897Z"), // start time 
"humidity" : 23 }

{ "_id" : ObjectId("585db63a41472bf7683eb3c6"), 
"datetime" : ISODate("2016-12-23T23:56:46.927Z"),  
"humidity" : 23 }

{ "_id" : ObjectId("585db63f41472bf7683eb3c7"), 
"datetime" : ISODate("2016-12-23T23:11:51.956Z"),  
"humidity" : 23 }

{ "_id" : ObjectId("585db644483ab4f778a635bf"), 
"datetime" : ISODate("2016-12-23T23:26:56.991Z"),  //end time
"humidity" : 23 }]

I found my answer here

Community
  • 1
  • 1
Ekom
  • 599
  • 3
  • 8
  • 24
  • 2
    Why are you sure there's something wrong? Can you please describe the problem further? – yorodm Dec 30 '16 at 16:21
  • My console log come out empty. Maybe the way it's organize might be wrong. I just need someone who has an idea of what i'm doing to point me in the right direction. – Ekom Dec 30 '16 at 17:47
  • @yorodm did you help me take a look?? – Ekom Dec 30 '16 at 22:20
  • @chridam what do you think ? – Ekom Dec 31 '16 at 00:28
  • Would be great if you could update your question using the [edit] link to include some sample documents from your collection and also the sample JSON output you are expecting from the aggregation. – chridam Dec 31 '16 at 08:16
  • @chridam updated to show the requested details. – Ekom Dec 31 '16 at 18:52
  • Still searching for a response. I would appreciate any efforts. – Ekom Jan 02 '17 at 18:06

0 Answers0