Tuesday, 18 April 2017

Nodejs code crashing Mongodb server

I have roughly 300k documents in one of my collections in mongodb and this is all sitting in AWS. When I query for this particular route (thing2), it seems like it's too much for mongo to handle so the mongod service crashes. I have added a limit to my code but it's still not working. Ideally, I would like to be able to search for particular keywords but that part has been commented out for now.

var express = require('express')
var app = express()

app.get('/', function (req, res) {
    res.send('Hello World!')
});

app.get('/thing2', function(req, res) {

    var username = req.query.username
    var keyword = req.query.keyword

    var MongoClient = require('mongodb').MongoClient,
        assert = require('assert');
    var url = 'mongodb://localhost:27017/my_db';

    MongoClient.connect(url, function(err, db) {
        if (err) throw err;

        var collection = db.collection('my_collection');
        / * collection.find({ $and: [ 
          * {"text" : {$regex : ".*"+keyword+".*"}}, 
          *  {"username" : username}
          *  ] }).limit(5).toArray(function(err, docs) {
          */

        collection.find({"user.screen_name" : username}).limit(1).toArray(function(err, docs) {
            console.log("\n" + new Date().toLocaleString() + " " + username);

            if (!err) {
                console.log(docs.length)
            } else {
                console.log(err)
            }

            res.send(docs)
        });
    });
});

app.listen(80, function () {
    console.log('Listening on port 80!')
});

The error I see in my console is:

{ [MongoError: connection 0 to localhost:27017 closed]
  name: 'MongoError',
  message: 'connection 0 to localhost:27017 closed' }



via user1883614

No comments:

Post a Comment