Last active
September 7, 2022 08:33
-
-
Save puppycodes/019f599fb65bc86da056bdabab70a06e to your computer and use it in GitHub Desktop.
MongoDB Atlas Webhook Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'}"); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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