Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "_id":"5c59c35d8610f702d00e6f70",
- "ipAddress":"50.116.14.48",
- "startTime":"2018-02-06T12:01:59.000Z",
- "endTime":"2018-02-06T12:31:00.000Z",
- }
- [
- {
- "occurrences":1,
- "startWindow":"2018-02-06T12:00:00.000Z",
- "endWindow":"2018-02-06T12:15:00.000Z"
- },
- {
- "occurrences":1,
- "startWindow":"2018-02-06T12:15:01.000Z",
- "endWindow":"2018-02-06T12:30:00.000Z"
- },
- {
- "occurrences":1,
- "startWindow":"2018-02-06T12:30:01.000Z",
- "endWindow":"2018-02-06T12:45:00.000Z"
- }
- ]
- db.col.aggregate([
- {
- $project: {
- startTime: { $toLong: { $toDate: "$startTime" } },
- endTime: { $toLong: { $toDate: "$endTime" } }
- }
- },
- {
- $project: {
- ranges: [
- {
- start: { $subtract: [ "$startTime", { $mod: [ "$startTime", 900000 ] } ] },
- end: { $add: [ { $subtract: [ "$startTime", { $mod: [ "$startTime", 900000 ] } ] }, 900000 ] }
- },
- {
- start: { $subtract: [ "$endTime", { $mod: [ "$endTime", 900000 ] } ] },
- end: { $add: [ { $subtract: [ "$endTime", { $mod: [ "$endTime", 900000 ] } ] }, 900000 ] }
- }
- ]
- }
- },
- {
- $unwind: "$ranges"
- },
- {
- $group: {
- _id: "$ranges",
- count: { $sum: 1 }
- }
- },
- {
- $project: {
- _id: 0,
- occurences: "$count",
- startWindow: { $toString: { $toDate: "$_id.start" } },
- endWindow: { $toString: { $toDate: "$_id.end" } }
- }
- }
- ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement