Skip to content

Instantly share code, notes, and snippets.

@raphaellondner-mongodb
Last active March 31, 2018 00:12
Show Gist options
  • Select an option

  • Save raphaellondner-mongodb/b0431429aa8a36a6e36371cbda37f879 to your computer and use it in GitHub Desktop.

Select an option

Save raphaellondner-mongodb/b0431429aa8a36a6e36371cbda37f879 to your computer and use it in GitHub Desktop.

Revisions

  1. raphaellondner-mongodb revised this gist Mar 31, 2018. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions mongodb-atlas-lambda-step1.js
    Original file line number Diff line number Diff line change
    @@ -19,9 +19,9 @@ function processEvent(event, context, callback) {
    try {
    if (cachedDb == null) {
    console.log('=> connecting to database');
    MongoClient.connect(atlas_connection_uri, function (err, db) {
    cachedDb = db;
    return createDoc(db, jsonContents, callback);
    MongoClient.connect(atlas_connection_uri, function (err, client) {
    cachedDb = client.db('travel');
    return createDoc(cachedDb, jsonContents, callback);
    });
    }
    else {
  2. raphaellondner-mongodb revised this gist Mar 30, 2017. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion mongodb-atlas-lambda-step1.js
    Original file line number Diff line number Diff line change
    @@ -43,6 +43,8 @@ function createDoc (db, json, callback) {
    console.log("Kudos! You just created an entry into the restaurants collection with id: " + result.insertedId);
    callback(null, "SUCCESS");
    }
    db.close();
    //we don't need to close the connection thanks to context.callbackWaitsForEmptyEventLoop = false (above)
    //this will let our function re-use the connection on the next called (if it can re-use the same Lambda container)
    //db.close();
    });
    };
  3. raphaellondner-mongodb revised this gist Mar 30, 2017. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions mongodb-atlas-lambda-step1.js
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,9 @@ function processEvent(event, context, callback) {
    }
    }

    //the following line is critical for performance reasons to allow re-use of database connections across calls to this Lambda function and avoid closing the database connection. The first call to this lambda function takes about 5 seconds to complete, while subsequent, close calls will only take a few hundred milliseconds.
    context.callbackWaitsForEmptyEventLoop = false;

    try {
    if (cachedDb == null) {
    console.log('=> connecting to database');
  4. raphaellondner-mongodb revised this gist Mar 29, 2017. 1 changed file with 13 additions and 9 deletions.
    22 changes: 13 additions & 9 deletions mongodb-atlas-lambda-step1.js
    Original file line number Diff line number Diff line change
    @@ -13,15 +13,19 @@ function processEvent(event, context, callback) {
    }
    }

    try
    {
    MongoClient.connect(atlas_connection_uri, function(err, db)
    {
    createDoc(db, jsonContents, callback);
    });
    }
    catch(err)
    {
    try {
    if (cachedDb == null) {
    console.log('=> connecting to database');
    MongoClient.connect(atlas_connection_uri, function (err, db) {
    cachedDb = db;
    return createDoc(db, jsonContents, callback);
    });
    }
    else {
    createDoc(cachedDb, jsonContents, callback);
    }
    }
    catch (err) {
    console.error('an error occurred', err);
    }
    }
  5. raphaellondner-mongodb revised this gist Mar 8, 2017. No changes.
  6. raphaellondner-mongodb revised this gist Feb 24, 2017. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions mongodb-atlas-lambda-step1.js
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@ function processEvent(event, context, callback) {
    {
    MongoClient.connect(atlas_connection_uri, function(err, db)
    {
    createDoc(db, jsonContents);
    createDoc(db, jsonContents, callback);
    });
    }
    catch(err)
    @@ -26,14 +26,16 @@ function processEvent(event, context, callback) {
    }
    }

    function createDoc (db, json) {
    function createDoc (db, json, callback) {
    db.collection('restaurants').insertOne( json, function(err, result) {
    if(err!=null) {
    console.error("an error occurred in createDoc", err);
    callback(null, JSON.stringify(err));
    }
    else {
    console.log("Kudos! You just created an entry into the restaurants collection with id: " + result.insertedId);
    callback(null, "SUCCESS");
    }
    db.close();
    });
    };
    };
  7. raphaellondner-mongodb created this gist Feb 21, 2017.
    39 changes: 39 additions & 0 deletions mongodb-atlas-lambda-step1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    function processEvent(event, context, callback) {
    console.log('Calling MongoDB Atlas from AWS Lambda with event: ' + JSON.stringify(event));
    var jsonContents = JSON.parse(JSON.stringify(event));

    //date conversion for grades array
    if(jsonContents.grades != null) {
    for(var i = 0, len=jsonContents.grades.length; i < len; i++) {
    //use the following line if you want to preserve the original dates
    //jsonContents.grades[i].date = new Date(jsonContents.grades[i].date);

    //the following line assigns the current date so we can more easily differentiate between similar records
    jsonContents.grades[i].date = new Date();
    }
    }

    try
    {
    MongoClient.connect(atlas_connection_uri, function(err, db)
    {
    createDoc(db, jsonContents);
    });
    }
    catch(err)
    {
    console.error('an error occurred', err);
    }
    }

    function createDoc (db, json) {
    db.collection('restaurants').insertOne( json, function(err, result) {
    if(err!=null) {
    console.error("an error occurred in createDoc", err);
    }
    else {
    console.log("Kudos! You just created an entry into the restaurants collection with id: " + result.insertedId);
    }
    db.close();
    });
    };