Last active
September 29, 2022 02:26
-
-
Save hemeda3/574fbcd686507c6829d0ac3688d6c9cd to your computer and use it in GitHub Desktop.
MongoDB+NodeJs script, that take string date convert it to ISO date, then extract ISO week number then count total per weekNo then calc average, then convert weeknumber to date range for readability
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```javascript | |
var mongoose = require('mongoose'); | |
//Set up default mongoose connection | |
async function calcSumByWeek() { | |
const weeklypipeline = [ | |
{ | |
$project: { | |
_id: 1, | |
resultList: 1 | |
} | |
}, | |
{ | |
$unwind: '$resultList' | |
}, | |
{ | |
$project: | |
{ | |
pk: {$arrayElemAt: ["$resultList", 0]}, | |
created_at: {$arrayElemAt: ["$resultList", 1]} | |
} | |
}, | |
{ | |
"$addFields": { | |
"created_at": { | |
"$toDate": "$created_at" | |
} | |
} | |
}, | |
{$project: {pk: 1, creationWeek: {$week: "$created_at"}}}, | |
{"$group": {_id: "$creationWeek", count: {$sum: 1}}}, | |
{$sort: {_id: 1}} | |
]; | |
// create custom connection | |
const Conn = mongoose.createConnection(); | |
// connect to database | |
await Conn.openUri('mongodb://127.0.0.1:27017/test2'); | |
// @type {AggregationCursor} | |
const addresses = Conn.collection('sam2').aggregate(weeklypipeline); | |
const newAddress = await addresses.toArray(); | |
const v2 = newAddress.map((a)=>{ | |
const newAdd = {}; | |
newAdd.isoWeekNo = a._id; | |
newAdd.total =a.count; | |
newAdd.dateRange = getDateRangeOfWeek(a._id); | |
return newAdd; | |
}) | |
console.log( v2); | |
} | |
async function averge() { | |
const pipelineaverge = [ | |
{ | |
$project: { | |
_id: 1, | |
resultList: 1 | |
} | |
}, | |
{ | |
$unwind: '$resultList' | |
}, | |
{ | |
$project: | |
{ | |
pk: {$arrayElemAt: ["$resultList", 0]}, | |
created_at: {$arrayElemAt: ["$resultList", 1]} | |
} | |
}, | |
{ | |
"$addFields": { | |
"created_at": { | |
"$toDate": "$created_at" | |
} | |
} | |
}, | |
{$project: {pk: 1, creationWeek: {$week: "$created_at"}}}, | |
{"$group": {_id: "$creationWeek", countTotalPerWeek: {$sum: 1}}}, | |
{"$group": {_id: null, theAverage: { $avg: "$countTotalPerWeek" }}}, | |
]; | |
// create custom connection | |
const Conn = mongoose.createConnection(); | |
// connect to database | |
await Conn.openUri('mongodb://127.0.0.1:27017/test2'); | |
// @type {AggregationCursor} | |
const addresses = Conn.collection('sam2').aggregate(pipelineaverge); | |
console.log( await addresses.toArray() ); | |
} | |
Date.prototype.getWeek = function () { | |
var target = new Date(this.valueOf()); | |
var dayNr = (this.getDay() + 6) % 7; | |
target.setDate(target.getDate() - dayNr + 3); | |
var firstThursday = target.valueOf(); | |
target.setMonth(0, 1); | |
if (target.getDay() != 4) { | |
target.setMonth(0, 1 + ((4 - target.getDay()) + 7) % 7); | |
} | |
return 1 + Math.ceil((firstThursday - target) / 604800000); | |
} | |
function getDateRangeOfWeek(weekNo){ | |
var d1 = new Date(); | |
numOfdaysPastSinceLastMonday = eval(d1.getDay()- 1); | |
d1.setDate(d1.getDate() - numOfdaysPastSinceLastMonday); | |
var weekNoToday = d1.getWeek(); | |
var weeksInTheFuture = eval( weekNo - weekNoToday ); | |
d1.setDate(d1.getDate() + eval( 7 * weeksInTheFuture )); | |
var rangeIsFrom = eval(d1.getMonth()+1) +"/" + d1.getDate() + "/" + d1.getFullYear(); | |
d1.setDate(d1.getDate() + 6); | |
var rangeIsTo = eval(d1.getMonth()+1) +"/" + d1.getDate() + "/" + d1.getFullYear() ; | |
return rangeIsFrom + " to "+rangeIsTo; | |
}; | |
calcSumByWeek().then(r => console.log("") ); | |
averge().then(r => console.log("") ); | |
``` |
sample data
{
"query": "ssssssssss",
"executionTime ":2319,
"resultCount ":0,
"exception ":null,
"resultList": [
["9469999954894634", "2022-09-21T12:21:18.360"],
["9469999955418922", "2022-09-21T12:06:27.927"],
["9469999955451690", "2022-09-21T12:20:24.737"],
["9469999955910442", "2022-09-21T18:47:42.022"],
["9469999955975978", "2022-09-21T15:33:46.790"],
["9469999956107050", "2022-09-21T13:38:05.998"],
["9471055527978", "2022-09-28T10:54:31.371"]
], "headers": ["xxx", "yyy"], "zzz": false]"
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
result: