3

I’m running mongoDB as a docker container locally, and I initiate a database creation when starting the container. But I can’t seem to connect to the database from my nodejs application when I add path to the dB. For example i can connect when I use “mongoDB://admin:password@localhost:27017” but not when I use “mongoDB://admin:password@localhost:27017/myDB”.

I'm running the container with docker-compose. Also when I log the output of running the mongo container I see a failure message that says:

"result":"UserNotFound: Could not find user \"admin\" for db \"journalDB\""}}

JournalDB is the name of the db i want to connect to, and i want to be able to connect to it from my nodejs application like so:

mongoose
.connect("mongoDB://admin:password@localhost:27017/journalDB", {
  useNewUrlParser: true,
  useUnifiedTopology: true,
})

I have read the answer to a similar question here and this documentation , but I feel they have only explained the possible problem, but have not helped me fix it. I would really appreciate it if there's a straightforward solution.

1 Answers1

3

Maybe try .connect("mongoDB://admin:password@localhost:27017/journalDB?authSource=admin" ...

Makariy
  • 725
  • 7
  • 16
  • Oh, this worked perfectly! I think I have been misunderstanding the answer in the similar question I shared as well as the documentation. They all mentioned adding authSource=admin but I had thought they meant replacing admin with my authentication db which I thought was supposed to be journalDB. Thank you so much for your clear answer! – Aso Amarachi Sep 04 '21 at 05:28