Skip to content

Instantly share code, notes, and snippets.

@RWaltersMA
Created September 5, 2018 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save RWaltersMA/163d90adc84419c4bf7b72bd058bece5 to your computer and use it in GitHub Desktop.
Save RWaltersMA/163d90adc84419c4bf7b72bd058bece5 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);
}
}
};
@chukwu1455
Copy link

Thank you.

I ran the code and I got the following error:

`> ran on Tue Nov 10 2020 15:38:49 GMT+0100 (West Africa Standard Time)

took 376.846231ms
error:
uncaught promise rejection: multiple write errors: [{write errors: [{cannot compare to undefined}]}, {}]`

Please, how can I resolve this?
Thank you.

@RWaltersMA
Copy link
Author

This code is to be run inside a MongoDB Atlas Function (former known as MongoDB Stitch). Were you trying to run this on its own?

@chukwu1455
Copy link

This code is to be run inside a MongoDB Atlas Function (former known as MongoDB Stitch). Were you trying to run this on its own?

No! I ran it inside a MongoDB Atlas Function. Although the challenge I have is that, I cannot see my data inside my collection ('sensordata'). Because I followed all instructions as directed. Is there anything else I'm supposed to do for documents to be inserted into my collection?

My sensor is capturing data and I could see it using MQTT Explorer but can't see them in my database.

I will appreciate your help. Thank you.

@chukwu1455
Copy link

This code is to be run inside a MongoDB Atlas Function (former known as MongoDB Stitch). Were you trying to run this on its own?

No! I ran it inside a MongoDB Atlas Function. Although the challenge I have is that, I cannot see my data inside my collection ('sensordata'). Because I followed all instructions as directed. Is there anything else I'm supposed to do for documents to be inserted into my collection?

My sensor is capturing data and I could see it using MQTT Explorer but can't see them in my database.

I will appreciate your help. Thank you.

Although, I am new to this field but I believe in learning by doing. So far so good I think I am catching up. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment