Skip to content

Instantly share code, notes, and snippets.

@mussacharles60
Forked from RWaltersMA/webhook.js
Created January 21, 2021 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mussacharles60/3edfdef679fe9b29322e0467fc47d0c0 to your computer and use it in GitHub Desktop.
Save mussacharles60/3edfdef679fe9b29322e0467fc47d0c0 to your computer and use it in GitHub Desktop.
MongoDB Stitch webhook for IoT blog
exports = function(payload,response) {
const mongodb = context.services.get("mongodb-atlas");
const sensordata = mongodb.db("iotdb").collection("sensordata");
var body = {};
if (payload.body) {
try{
const document = EJSON.parse(payload.body.text());
//sample document:{ "device" : "TempSensor1", "sample_date" : "2018-09-03", "value" : "72.5", "time" : "1536011844" }
var sample={val:Number(document.value), time: Number(document.time) };
var day=new Date(document.sample_date);
sensordata.updateOne({"device":document.device,nsamples:{$lt:200},day:day},
{"$push":{samples:sample},
"$min":{first:sample.time},
"$max":{last:sample.time},
"$inc":{nsamples:Number(1)}},
{upsert:true}
).then(result => {
response.setStatusCode(201);
});
}
catch(err) {
console.log(err);
response.setStatusCode(500);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment