What is a "Connection" in MongoDB?

Viewed 1393

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!

2 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

More number of connections which you see in Atlas because there are some internal connections are also made in order to make the cluster running, these may include the connections from:

  • Connections made from a client.
  • Internal connections between primary and secondaries.
  • As it is a hosted service and everything is being monitored so connections from the monitoring agent.

  • As automation works, so the connections from the automation agent as well.

Hence whenever a new cluster is being created in Atlas, you will always see some connections in the metrics Page even though no client is being connected.

Related