Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot and how to configure connection details to MongoDB?

Being new to Spring Boot I am wondering on how I can configure connection details for MongoDB. I have tried the normal examples but none covers the connection details.

I want to specify the database that is going to be used and the url/port of the host that runs MongoDB.

Any hints or tips?

like image 380
Marco Avatar asked May 07 '14 10:05

Marco


People also ask

Does spring boot provide MongoDB auto configuration?

Spring Boot provides auto-configuration for Redis, MongoDB, Elasticsearch, Solr and Cassandra; you can make use of the other projects, but you will need to configure them yourself.

How do you establish connection to MongoDB?

To connect to your local MongoDB, you set Hostname to localhost and Port to 27017 . These values are the default for all local MongoDB connections (unless you changed them). Press connect, and you should see the databases in your local MongoDB.

Which annotation is used to link a MongoDB document with a spring bean?

The @Id annotation tells the mapper which property you want to use for the MongoDB _id property and the @Indexed annotation tells the mapping framework to call ensureIndex on that property of your document, making searches faster.


2 Answers

Just to quote Boot Docs:

You can set spring.data.mongodb.uri property to change the url, or alternatively specify a host/port. For example, you might declare the following in your application.properties:

spring.data.mongodb.host=mongoserver spring.data.mongodb.port=27017 

All available options for spring.data.mongodb prefix are fields of MongoProperties:

private String host;  private int port = DBPort.PORT;  private String uri = "mongodb://localhost/test";  private String database;  private String gridFsDatabase;  private String username;  private char[] password; 
like image 77
Artem Bilan Avatar answered Oct 11 '22 21:10

Artem Bilan


spring.data.mongodb.host and spring.data.mongodb.port are not supported if you’re using the Mongo 3.0 Java driver. In such cases, spring.data.mongodb.uri should be used to provide all of the configuration, like this:

spring.data.mongodb.uri=mongodb://user:[email protected]:12345 
like image 41
Ali Dehghani Avatar answered Oct 11 '22 20:10

Ali Dehghani