0

I'm using node 0.10.21 and mongodb-native (aka require('mongodb')).

The problem I'm having is, that I cannot aggregate a timeseries collection AND use match to select a certain time frame:

var start = new Date(); //just demo, the start date is actually lower than NOW()
var end = new Date(); // is usally NOW() in my queries
$match : [{'$match' : {'date' : {'$gte' : start, '$lte' : end}}}]

The the generated query (JSON.stringify(query);) looks like this:

{"$match":{"date":{"$gte":"2013-11-09T23:00:00.000Z","$lte":"2013-11-12T05:00:00.000Z"}}}

Of course, I've read the docs at http://mongodb.github.io/node-mongodb-native/api-articles/nodekoarticle1.html#mongo-db-data-types and the docs say:

Date maps directly to a Javascript Date

Obviously not, or I'm overlooking something. I've seen similar questions regarding this issue, but none of them provide a solution.

In MongoDB shell everything works fine using {date : {'$gte' : new Date(2013,10,25)}} - so the collection itself is full of valid data that I should be able to query using node. I've also written a PHP-Script doing the same type of query and it works fine. Hence I suspect I either misread the docs, or the mongodb-native driver does not map JavaScript's Date() to MongoDB's Date().

Can someone please tell me how to query for a date range?

Update1: more Code

Here's a bit more of the code I'm using, as requested by JohnnyHK. I've stripped out the code that builds the actual aggregation values - those work as expected, only the $match part is giving me headaches.

var start = new Date(start_date);
var end =  new Date(end_date); 
console.log(start,end);
//outputs: 2013-11-10 0:00 2013-11-12 6:00


var ops = [{'$match' : {'date' : { '$gte' : start, '$lte' : end} }}, {'$group' : group_values}];
console.log(JSON.stringify(ops));

//outputs: [{"$match":{"date":{"$gte":"2013-11-09T23:00:00.000Z","$lte":"2013-11-12T05:00:00.000Z"}}},{"$group":{"used":{"$avg":"$used"},"system":{"$avg":"$system"},"iowait":{"$avg":"$iowait"},"_id":{"group_id":{"$subtract":[{"$divide":["$timestamp",3600]},{"$mod":[{"$divide":["$timestamp",3600]},1]}]}}}}]


collection.aggregate(ops,function(err, results) {
    console.log('mongo results');
    console.log(arguments);
//output: mongo results
//{ '0': null, '1': [] }

});

Update2

Sample Data:

{ "date" : ISODate("2013-10-22T13:52:16Z"), "timestamp" : 1382449936, "used" : 743.7768451188878, "system" : 855.7432519109785, "iowait" : 0, "max" : 400, "_id" : ObjectId("5266831aacce8ec133bb11af") }
{ "date" : ISODate("2013-10-22T13:52:06Z"), "timestamp" : 1382449926, "used" : 758.1516303260652, "system" : 840.5681136227246, "iowait" : 0, "max" : 400, "_id" : ObjectId("5266831aacce8ec133bb11ae") }
{ "date" : ISODate("2013-10-22T13:51:56Z"), "timestamp" : 1382449916, "used" : 826.0163785417379, "system" : 765.362035769591, "iowait" : 1.9952086438206234, "max" : 400, "_id" : ObjectId("52668306acce8ec133bb10f7") }
{ "date" : ISODate("2013-10-22T13:51:46Z"), "timestamp" : 1382449906, "used" : 1079.9727415609912, "system" : 526.4513588679281, "iowait" : 0, "max" : 400, "_id" : ObjectId("52668306acce8ec133bb10f6") }
{ "date" : ISODate("2013-10-22T13:51:36Z"), "timestamp" : 1382449896, "used" : 1327.1878627467097, "system" : 273.2917036211522, "iowait" : 0, "max" : 400, "_id" : ObjectId("526682f2acce8ec133bb1043") }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Steffen
  • 589
  • 6
  • 16
  • JSON doesn't have a `Date` data type, so the `JSON.stringify(query)` output you're seeing is expected. As you say, you're overlooking something else. Can you update your question to include more of your `aggregate` call code? – JohnnyHK Nov 26 '13 at 13:30
  • done - I've updated my post. I've also double checked that I'm not overriding the vars start/end and they are both Date() objects as shown in the example above. – Steffen Nov 26 '13 at 14:14
  • Thanks for adding the code; we also need a sample document from your collection that you expect to be matched. – JohnnyHK Nov 26 '13 at 14:16
  • 1
    The `date` values in your sample data are from October, but you're matching dates in November so I wouldn't expect any matches. If I change the range to include October it works. – JohnnyHK Nov 26 '13 at 15:33

0 Answers0