node.js - How to save in MongoDB with timestamp and then retrieve results by week -
node.js - How to save in MongoDB with timestamp and then retrieve results by week -
i have snippet right here saves user in mongodb:
router.post('/adduser', function(req, res) { var db = req.db; var document = req.body; var coderesponse = code(); var timestamp = new date(); document.code = coderesponse; document.timestamp = timestamp; db.collection('userlist').insert(document, function(err, result){ res.send( (err === null) ? { msg: '',code: coderesponse } : { msg: err } ); }); });
as can see generates timestamp via new date();
function in js.
how can retrieve users week?
i tried this:
router.get('/winnerlist', function(req, res) { var db = req.db; var start = new date(2014, 6, 14); var end = new date(2014, 6, 29); db.collection('userlist').find({"timestamp": {"$gte": start, "$lt": end}}).toarray(function (err, items) { res.json(items); }); });
you can week number date object , after can insert week number new property, i've found useful method
date.prototype.getweek = function() { var onejan = new date(this.getfullyear(),0,1); homecoming math.ceil((((this - onejan) / 86400000) + onejan.getday()+1)/7); } var today = new date(); var week = today.getweek(); console.log(week); //which homecoming 26 today
node.js mongodb express
Comments
Post a Comment