Spring boot + Mongodb connection problems
larskarlsen
HOBBYOP

3 months ago

Hi. I am trying to connect my spring boot application with a Mongodb database. Locally I have got it to work by setting this in my application.yml file:

spring.mongodb.uri=${MONGODB_URI:mongodb://root:example@localhost:27017/testdb?authSource=admin}

When I'm trying to run the same spring boot application in railway with a variable the application doesn't start up as the class that connects to the database can't set up. I have manually connect to the mongodb database via MongoDB Compass and created <my db> and created a collection with the same name as the one specified by my model in Spring (@Document(collection = "products"))

This is the variable i have in my railway spring boot app:
MONGODB_URI=${{MongoDB.MONGO_URL}}/<my db>?authSource=admin

Solved$10 Bounty

Pinned Solution

larskarlsen
HOBBYOP

3 months ago

locally it seems like spring.mongodb.uri is the correct key. When i change for example the port in the URI, the log seems to change port aswell.
After some debugging locally i found out that the issue was the version of the dependency spring-boot-starter-data-mongodb. Before i had it to 3.x.xand when i changed it to 4.0.0 it started working on railway.

4 Replies

domehane
FREETop 10% Contributor

3 months ago

change spring.mongodb.uri to spring.data.mongodb.uri in your application.yml ; you're missing the .data part

i think that's why it's not picking up the connection string in railway


larskarlsen
HOBBYOP

3 months ago

locally it seems like spring.mongodb.uri is the correct key. When i change for example the port in the URI, the log seems to change port aswell.
After some debugging locally i found out that the issue was the version of the dependency spring-boot-starter-data-mongodb. Before i had it to 3.x.xand when i changed it to 4.0.0 it started working on railway.


domehane
FREETop 10% Contributor

3 months ago

ok so it was actually a version compatibility issue with railway's mongodb setup


domehane

ok so it was actually a version compatibility issue with railway's mongodb setup

larskarlsen
HOBBYOP

3 months ago

Correct


Status changed to Solved brody 3 months ago


Loading...