Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a "Connection" in MongoDB?

I've been working with MongoDB for a while now and I've been liking it a lot. One thing I do not understand however is "Connections". I've searched online and everything just has very vague and basic answers. I'm using MongoDBs cloud service called "Atlas" and it describes the connection count as

The number of currently active connections to this server. A stack is allocated per connection; thus very many connections can result in significant RAM usage.

However I have a few questions.

What is a connection I guess? As I understand it, a connection is made between the server and the database service. Essentially when I use mongoose.connect(...);, a connection is made. So at most, there should only be one connection. However when I was testing my program I noticed my connection count was at 2 and in some moments it spiked up all the way to 7 and went to 5 and fluctuated. Does a "connection" have anything to do with the client? On the dashboard of Atlas it says I have a max connection amount of 500. What does this value represent? Does this mean only 500 users can use my website at once? If that's the case, how can I increase that number? Or how can I make sure that more than 500 connections never get passed? Or is a connection something that gets opened and I have to manually close myself? Because I've been learning from tutorials and I've never seen/heard anything like that.

Thanks!

like image 233
WildWombat Avatar asked Mar 31 '20 01:03

WildWombat


1 Answers

mongoose.connect doesn't limit itself to 1 connection to the Mongo Server.

By default, mongoose creates a pool of 5 connections to Mongo.

You can change this default if necessary.

mongoose
    .connect(mongoURI, {poolSize : 200});

See https://mongoosejs.com/docs/connections.html

like image 168
grantnz Avatar answered Oct 14 '22 06:10

grantnz