Skip to content

Commit

Permalink
Using _id for querying
Browse files Browse the repository at this point in the history
  • Loading branch information
Mircea Danila Dumitrescu committed May 8, 2014
1 parent c83d711 commit 80031bc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion examples/event-stream/aggregation-get.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ <h1>Streaming Aggregations</h1>
console.log("connected!");
socket.send(JSON.stringify({
name: aggregation,
start: new Date(new Date().getTime() - 15 * 60000)
start: new Date(new Date().getTime() - 30 * 60000)
}
));
};
Expand Down
9 changes: 5 additions & 4 deletions lib/cube/aggregator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
/*
* Aggregation have fixed times of execution: 1m and 5m
*/
var util = require('util');
var lock = false;
var util = require('util'),
mongoutils = require("./mongoutils.js"),
lock = false;
//noinspection JSLint
exports.register = function (dbs, endpoints, options) {
if (dbs.length !== 2) {
Expand Down Expand Up @@ -60,7 +61,7 @@ exports.register = function (dbs, endpoints, options) {
}

fiveMinAggregation = [
{"$match": {t: {$in: [new Date(t0_5m), new Date(t0_5m + 60000), new Date(t0_5m + 120000), new Date(t0_5m + 180000), new Date(t0_5m + 240000)]}}},
{"$match": {_id: {$gte: mongoutils.objectIdFromDate(t0_5m), $lt: mongoutils.objectIdFromDate(t0_5m + 300000)}}},
{"$group": aggregationGroup}
];

Expand Down Expand Up @@ -126,7 +127,7 @@ exports.register = function (dbs, endpoints, options) {
t1_1m = new Date(t1_1m);
if (t1_1m < new Date()) {
//start aggregating
aggregation[0].$match.t = {"$gte": t0_1m, "$lt": t1_1m};
aggregation[0].$match._id = {"$gte": mongoutils.objectIdFromDate(t0_1m), "$lt": mongoutils.objectIdFromDate(t1_1m)};
//console.log(util.inspect(aggregation, { depth: null, colors: true}));
eventsDB.collection("events").aggregate(aggregation, {}, function (err, result) {
if (err) {
Expand Down
6 changes: 3 additions & 3 deletions lib/cube/genericGetter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";
var util = require("util"),
mongoutils = require("./mongoutils.js"),
mongodb = require("mongodb"),
type_re = /^[a-z][a-zA-Z0-9_]+$/,
MAX_RETURNED_RECORDS = 10000;
Expand Down Expand Up @@ -118,10 +119,9 @@ module.exports = function (db) {
}

// Copy any expression filters into the match object.
filter = {t: {$gte: start}};

filter = {_id: {$gte: mongoutils.objectIdFromDate(start)}};
if (stop !== undefined) {
filter.t.$lt = stop;
filter._id.$lt = mongoutils.objectIdFromDate(stop);
}

if (name === undefined) {
Expand Down
15 changes: 15 additions & 0 deletions lib/cube/mongoutils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";

var mongodb = require("mongodb");
module.exports.objectIdFromDate = function (ts) {
return new mongodb.ObjectID(Math.floor(ts / 1000).toString(16) + "0000000000000000");
};
module.exports.dateFromObjectId = function (obj) {
return obj.getTimestamp();
};
module.exports.epochFromObjectId = function (obj) {
return parseInt(obj.valueOf().slice(0, 8), 16);
};
module.exports.epochMSFromObjectId = function (obj) {
return parseInt(obj.valueOf().slice(0, 8), 16);
};

0 comments on commit 80031bc

Please sign in to comment.