I am using stitch function to get the last 7 days from event collection.below is my code.this is execute in stitch function.
exports = async function() {
var events = context.services.get("mongodb-atlas").db("utilo").collection("events");
var today = new Date();
var lastWeek = BSON.Timestamp(today.setDate(today.getDate()-7),0);
console.log(lastWeek);
var document = null;
do{
document = await cursor.next();
if(document){
var result = {};
result.community = document._id;
result.data.newUsersCount = await events.count({community:document._id,type:"join",status:"completed",ts:{$gt:lastWeek}});
}}
}
In the above code, I tried to get last 7 days records from event collection.Here (today.setDate(today.getDate()-7),0) getting the correct Timestamp value but after adding BSON.Timestamp, the timestamp will change to lower year or higher year like either 2004 or 2024. without changing the Timestamp value, can we convert to the Timestamp?
How can i store value in last week like TImestamp(1520801145,0)?
or How to write the code for get the last 7 days record from events collection (ts stored in timestamp)