-1

I would like to read a collection in mLab(mongoDB) and get result document based on the request from AWS LAMBDA function.

I could write a nodeJS function code snippet and whatever timeout I set it results in

Task timed out after *** seconds

Any solution, link or thoughts will be helpful. Either JAVA or NODE

    'use strict';
const MongoClient = require('mongodb').MongoClient;

exports.handler = (event, context, callback) => {
    console.log('=> connect to database');


    MongoClient.connect('mongodb://test:test123@ds.xyx.fleet.mlab.com:1234', function (err, client) {
        if (err) {
            console.log("ERR ",err );
            throw err;
        }

        var db = client.db('user');

        db.collection('sessions').findOne({}, function (findErr, result) {
            if (findErr){
                console.log("findErr ",findErr);
                throw findErr;
            } else {
                console.log("#",result);
                console.log("##",result.name);
                context.succeed(result);
            }
            client.close();
        });
    });
};

P.S : Referred all related stack questions.

Selvam Raju
  • 196
  • 4
  • 14

1 Answers1

0

Lambda function returned success after adding db name in

MongoClient.connect('mongodb://test:test123@ds.xyx.fleet.mlab.com:1234/dbNAME')

Apart from declaring db name in

var db = client.db('dbNAME');

It should also be added in mLab connection URI.

Selvam Raju
  • 196
  • 4
  • 14