30.2 MongoDB
MongoDB is an open-source NoSQL document database that uses a JSON-like schema instead of traditional table-based relational data. Spring Boot offers several conveniences for working with MongoDB, including the spring-boot-starter-data-mongodb
‘Starter’.
30.2.1 Connecting to a MongoDB database
You can inject an auto-configured org.springframework.data.mongodb.MongoDbFactory
to access Mongo databases. By default the instance will attempt to connect to a MongoDB server using the URL mongodb://localhost/test
:
import org.springframework.data.mongodb.MongoDbFactory; import com.mongodb.DB; _@Component_ public class MyBean { private final MongoDbFactory mongo; _@Autowired_ public MyBean(MongoDbFactory mongo) { this.mongo = mongo; } // ... public void example() { DB db = mongo.getDb(); // ... } }
You can set spring.data.mongodb.uri
property to change the URL and configure additional settings such as the replica set:
spring.data.mongodb.uri=mongodb://user:[email protected]:12345,mongo2.example.com:23456/test
Alternatively, as long as you’re using Mongo 2.x, 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
Note | |
---|---|
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. |
Tip | |
---|---|
If spring.data.mongodb.port is not specified the default of 27017 is used. You could simply delete this line from the sample above. |
Tip | |
---|---|
If you aren’t using Spring Data Mongo you can inject com.mongodb.Mongo beans instead of using MongoDbFactory . |
You can also declare your own MongoDbFactory
or Mongo
bean if you want to take complete control of establishing the MongoDB connection.
30.2.2 MongoTemplate
Spring Data Mongo provides a MongoTemplate
class that is very similar in its design to Spring’s JdbcTemplate
. As with JdbcTemplate
Spring Boot auto-configures a bean for you to simply inject:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.stereotype.Component; _@Component_ public class MyBean { private final MongoTemplate mongoTemplate; _@Autowired_ public MyBean(MongoTemplate mongoTemplate) { this.mongoTemplate = mongoTemplate; } // ... }
See the MongoOperations
Javadoc for complete details.
30.2.3 Spring Data MongoDB repositories
Spring Data includes repository support for MongoDB. As with the JPA repositories discussed earlier, the basic principle is that queries are constructed for you automatically based on method names.
In fact, both Spring Data JPA and Spring Data MongoDB share the same common infrastructure; so you could take the JPA example from earlier and, assuming that City
is now a Mongo data class rather than a JPA @Entity
, it will work in the same way.
package com.example.myapp.domain; import org.springframework.data.domain.*; import org.springframework.data.repository.*; public interface CityRepository extends Repository<City, Long> { Page<City> findAll(Pageable pageable); City findByNameAndCountryAllIgnoringCase(String name, String country); }
Tip | |
---|---|
For complete details of Spring Data MongoDB, including its rich object mapping technologies, refer to their reference documentation. |
30.2.4 Embedded Mongo
Spring Boot offers auto-configuration for Embedded Mongo. To use it in your Spring Boot application add a dependency on de.flapdoodle.embed:de.flapdoodle.embed.mongo
.
The port that Mongo will listen on can be configured using the spring.data.mongodb.port
property. To use a randomly allocated free port use a value of zero. The MongoClient
created by MongoAutoConfiguration
will be automatically configured to use the randomly allocated port.
If you have SLF4J on the classpath, output produced by Mongo will be automatically routed to a logger named org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongo
.
You can declare your own IMongodConfig
and IRuntimeConfig
beans to take control of the Mongo instance’s configuration and logging routing.