8

I am facing a really weird scenario, the initial connection to mongoDb takes around 15 seconds. My current setup is the following:

  1. mongodb running inside an ubuntu vm on the same machine
  2. mongodb is version 2.6.1
  3. node.js installed using brew and it is version 0.10.28

Upon restarting nodemon the initial signin POST takes around 15 seconds

POST /api/v1/signin 200 14707ms - 56b

other POST to the same route without restarting the server is relatively fast:

POST /api/v1/signin 200 76ms - 56b

the reason why this bothers me is that because this project is still in development, nodemon tends to restart a lot and testing is being a pain.

I am using the following node modules which are related to db and authentication:

  1. "express": "~4.2.0",
  2. "mongoose": "3.8.8",
  3. "passport": "0.2.0",
  4. "passport-local": "0.1.6",
  5. "bcrypt": "*"

this is the way i am connecting to mongo:

var mongoUrl = "mongodb://devmachine.local:27017/project";
mongoose.connect(mongoUrl, {auto_reconnect: true});

any help would be highly appreciated.

Thanks

laggingreflex
  • 32,948
  • 35
  • 141
  • 196
Fouad
  • 855
  • 1
  • 11
  • 30

2 Answers2

4

Replace the hostname with IP

From: var mongoUrl = "mongodb://devmachine.local:27017/project";

To: var mongoUrl = "mongodb://127.0.0.1:27017/project";

  • +1 In my case I couldn't use the IP due to using TLS, but adding the IP and DNS in the `/etc/hosts` file solved the issue. – user000001 Apr 15 '20 at 07:23
-1

This would disturb your backend's functionality since the connection isn't right.

change the mongoURI to "mongodb://127.0.0.1:27017"

If this doesn't even work, try installing the MongoDB community server from here: https://www.mongodb.com/try/download/community

This also runs a MongoDB service in your machine, and that's what solved it for me. Remember to restart your backend after completing the steps.

  • I’m sorry but this is not a relevant answer – Fouad Mar 13 '23 at 20:37
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 19 '23 at 21:10