Skip to content

Instantly share code, notes, and snippets.

@maswadkar
Last active July 2, 2019 18:39
Show Gist options
  • Save maswadkar/4081a0ae96b33afd6040a057a8762255 to your computer and use it in GitHub Desktop.
Save maswadkar/4081a0ae96b33afd6040a057a8762255 to your computer and use it in GitHub Desktop.
azure function - mongodb
// credit goes to
//https://thecodebarbarian.com/getting-started-with-azure-functions-and-mongodb
const mongodb = require('mongodb');
// URI for MongoDB Atlas
const uri = 'mongodb://test.mongodb.net:27017';
module.exports = function (context, req) {
context.log('Running');
mongodb.MongoClient.connect(uri, function(error, client) {
if (error) {
context.log('Failed to connect');
context.res = { status: 500, body: res.stack }
return context.done();
}
context.log('Connected');
client.db('test').collection('tests').find().toArray(function(error, docs) {
if (error) {
context.log('Error running query');
context.res = { status: 500, body: res.stack }
return context.done();
}
context.log('Success!');
context.res = {
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ res: docs })
};
context.done();
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment