Skip to content

Instantly share code, notes, and snippets.

@puppycodes
Last active September 7, 2022 08:33
Show Gist options
  • Save puppycodes/019f599fb65bc86da056bdabab70a06e to your computer and use it in GitHub Desktop.
Save puppycodes/019f599fb65bc86da056bdabab70a06e to your computer and use it in GitHub Desktop.
MongoDB Atlas Webhook Functions
exports = function(payload, response) {
// declare mongo services and db
const mongodb = context.services.get('<your-service-name>');
const collection = mongodb.db('<your-db-name>').collection('<your-collection-name>');
// json from webhook into insert function...
// convert body to EJSON for proper formatting
collection.insertOne({
body: EJSON.parse(payload.body.text()),
query: payload.query
}).then(result => {
// respond with result
response.setStatusCode(201);
response.setBody("{'message': 'success!'}");
});
};
exports = function(payload, response) {
// declare mongo services and db
const mongodb = context.services.get('<your-service-name>');
const mycollection = mongodb.db('<your-db-name>').collection('<your-collection-name>');
// find all documents in entire collection, return them as an array
return mycollection.find({
}).toArray();
};
exports = function(payload, response) {
// declare mongo services and db
const mongodb = context.services.get('<your-service-name>');
const collection = mongodb.db("<your-db-name>").collection("<your-collection-name>");
var json = EJSON.parse(payload.body.text());
var user = json.userId;
var status = json.orderStatus;
// json from webhook into update function...
// replaces values in document when userId field matches existing data.
return collection
.updateOne(
{"body.userId": user},
{$set:
{
"body": json,
"query": payload.query
}
},
{upsert: true}
)
.then(result => {
// return the result to webhook response
return result;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment