11

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!

6
  • It depends. Do you create a new mongo connection on each request? This is the way PHP and other stateless languages do it, but typically in Node you can create one global pool of connections and re-use that for clients.
    – mpen
    Commented Mar 31, 2020 at 1:09
  • I don't believe I do but I could be wrong. I have my schema and connect to the database in the beginning. Then on each request (that requires info from my db) I simply query the db like User.find(...) and that's it.
    – WildWombat
    Commented Mar 31, 2020 at 1:12
  • Is it a single connection or a connection pool? I'm unfamiliar with mongo & mongoose but with MySQL I always create a pool of connections, so if there's already one outgoing query it'll create a 2nd connection, even if there's only 1 client -- that way I can run multiple queries at once.
    – mpen
    Commented Mar 31, 2020 at 1:15
  • A connection is 1 TCP connection to the port the mongod is listening on.
    – Joe
    Commented Mar 31, 2020 at 2:11
  • Sorry I'm not sure I quite get it still. The connection means 1 connection to the mongodb server? So I'm working on an app and I did one query and I somehow jumped to 7 connections and it's staying there. Do I have to manually close connections? And how did 7 connections open up
    – WildWombat
    Commented Mar 31, 2020 at 3:24

2 Answers 2

2

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

1

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.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.