1

Have to improve on my current Express/Mongoose server, thinking I may need to use mongoose.createConnection over mongoose.connect, and perhaps maintaining an array of connections, and deleting the indexes as the connection dies - so I need some input from your collective minds.

The script is essentially built up like this:

// Constants
var MONGO_SERVER = "mongodb://user:pass@localhost/dbname";

// Connection Handlers
function checkConnection(cb) {
    if (mongoose.Connection.STATES.connected !== mongoose.connection.readyState) connectMongo(cb());
    else cb();
}
function connectMongo(cb) {
    db = mongoose.connect(MONGO_SERVER,function(err){if (err) throw err; else if (typeof cb==="function") cb();});
}

// Setup connection
mongoose = require('mongoose');
var db = {};
connectMongo();
Schema = mongoose.Schema;
ObjectId = Schema.ObjectId;

// Schemas...
var UsersSchema = new Schema({
    display_name    : String,
    email           : String
}); 
//...
var users = mongoose.model('Users',UsersSchema);

//...
function login(conn,obj,link_id) {
    checkConnection(function() {
        try {   
            //...
        }
        catch (ex) {
            console.log(ex.message);
        }
    });
}

Now, this works, but seeing some performance issues and some queries appear to be hanging tells me I need to re-shape this type of execution, and maybe making var db = {} to be an array, and using createConnection pushed to the end of it. Don't need a direct answer, just a nudge in the right direction.

Thanks.

2
  • 4
    Unless I'm missing some part of the question, doesn't mongoose's connection pool handle asynchronous connections automatically? See the connection docs. Commented Feb 14, 2016 at 14:41
  • 2
    As Ashley B said, Mongoose handle all these async thing. Still not satisfied, you can mongoose-and-multiple-database-in-single-node-js-project Commented Feb 14, 2016 at 14:58

1 Answer 1

3

Write-up from my comment:

Mongoose handles async connections by default using connection pools. You can specify the pool size like so:

var options = {
  server: { poolSize: 10 } // default is '5'
}

mongoose.connect('mongodb://username:password@host:port/database', options);

A connection will be used from the connection pool and then released back to the pool once the transaction has been completed.

Sign up to request clarification or add additional context in comments.

2 Comments

Cheers Ashley - that's pretty much what I needed to know! And the tips from that other page Deepak was very useful. With the changes made in the options object, working a lot more smoother now.
No problem, if you want to make it a bit cleaner (if you have no other options) you can also just add it to the mongo connection uri like so: mongodb://username:password@host:port/database?poolSize=4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.