1
\$\begingroup\$

I am currently struggling with a request I am making to my server that is taking more than 4 seconds! I cut out a lot of the cruft to hopefully make it easier to read. Excuse my horrible variable names. I looked for a while online and it seems that .toArray() is slow for MongoDB. However I would defer to someone with more knowledge.

socket.on('userInput', function (exampleMarket) {

    //example of a collection name
    //var exampleMarket = 'FTSE100';
    var red = {};

    APP.db2.collection(exampleMarket + '-market', function(err, res) {

        if (res === null) {
            logger.log('no match in database 1')
            return
        }

        res.find().toArray(function (err,doc) {

            red=doc;

        });

    });

    var green= {

        collection  : [],
        type        : 'market',
        open        : [],
        high        : [],
        low         : [],
        volume      : [],
        close       : [],
        date        : [],
        code        : [],
        description : [],
        frequency   : [],
        fromDate    : [],
        lastUpdate  : [],
        toDate      : [],
        name        : [],
        color       : [],
        movement    : [],
        difference  : [],
        point       : [],
        tail        : [],
        chartdata   : {
            xaxis   : [],
            yaxis   : []
        }

    };

    APP.db.collection(exampleMarket, function(err, res) {

        if (res === null) {
            logger.log('no match in database 1')
            return
        }

        //logs fast
        logger.log('test1')
        socket.emit('Share', 'test1')

        res.find().toArray(function (err,doc) {
            //No errors
            logger.log(err)

            for (var i = 0; i < doc.length; i++) {

                if (doc[i].code === undefined) {
                    green.marketOverview = doc[i].market;
                    continue;

                }

                green.collection.push(exampleMarket);
                green.chartdata.xaxis.push(doc[i].date);
                green.chartdata.yaxis.push(doc[i].open);
                green.name.push(doc[i].name);

                green.toDate.push(doc[i].toDate);
                green.color.push(doc[i].color);
                green.movement.push(doc[i].movement);
                green.difference.push(doc[i].difference);
                green.point.push(doc[i].point);
                green.tail.push(doc[i].tail);

                green.open.push(doc[i].open[0]);
                green.date.push(doc[i].date[0]);
                green.high.push(doc[i].high[0]);
                green.low.push(doc[i].low[0]);
                green.volume.push(doc[i].volume[0]);
                green.close.push(doc[i].close[0]);
                green.code.push(doc[i].code);

            }

            green.marketName = exampleMarket;

            socket.emit('Share', green, red);

            //this takes about 3 seconds to log out
            logger.log('test2')
            socket.emit('Share', 'test2')

        });
        //logs fast
        socket.emit('Share', 'test3')
    });


});
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.