The primary goal of the Spring Data project is to make it easier to build Spring-powered applications that use new data access technologies such as non-relational databases, map-reduce frameworks, and cloud based data services.
The Spring Data project aims to provide a familiar and consistent Spring-based programming model for new datastores while retaining store-specific features and capabilities.
The Spring Data MongoDB project provides integration with the MongoDB document database.
Key functional areas of Spring Data MongoDB are a POJO centric model for interacting with a MongoDB Document and easily writing a repository style data access layer.
Here is a quick teaser of an application using Spring Data Repositories in Java:
public interface PersonRepository extends CrudRepository<Person, Long> {
List<Person> findByLastname(String lastname);
List<Person> findByFirstnameLike(String firstname);
}
@Service
public class MyService {
private final PersonRepository repository;
public MyService(PersonRepository repository) {
this.repository = repository;
}
public void doWork() {
repository.deleteAll();
Person person = new Person();
person.setFirstname("Oliver");
person.setLastname("Drotbohm");
repository.save(person);
List<Person> lastNameResults = repository.findByLastname("Drotbohm");
List<Person> firstNameResults = repository.findByFirstnameLike("Oli*");
}
}
@Configuration
@EnableMongoRepositories
class ApplicationConfig extends AbstractMongoClientConfiguration {
@Override
protected String getDatabaseName() {
return "springdata";
}
}The spring.io site contains several guides that show how to use Spring Data step-by-step:
-
Accessing Data with MongoDB is a very basic guide that shows you how to create a simple application and how to access data using repositories.
-
Accessing MongoDB Data with REST is a guide to creating a REST web service exposing data stored in MongoDB through repositories.
In order to build Spring Data MongoDB, you will need to download and install a MongoDB distribution and Docker.
Once you have installed MongoDB, you need to start a MongoDB server. It is convenient to set an environment variable to
your MongoDB installation directory (e.g. MONGODB_HOME).
To run the full test suite, a MongoDB Replica Set is required.
To run the MongoDB server enter the following command from a command-line:
$ $MONGODB_HOME/bin/mongod --dbpath $MONGODB_HOME/runtime/data --ipv6 --port 27017 --replSet rs0
...
"msg":"Successfully connected to host"Once the MongoDB server starts up, you should see the message (msg), "Successfully connected to host".
Notice the --dbpath option to the mongod command. You can set this to anything you like, but in this case, we set
the absolute path to a sub-directory (runtime/data/) under the MongoDB installation directory (in $MONGODB_HOME).
You need to initialize the MongoDB replica set only once on the first time the MongoDB server is started. To initialize the replica set, start a mongo client:
$ $MONGODB_HOME/bin/mongo
MongoDB server version: 8.0.0
...Then enter the following command:
mongo> rs.initiate({ _id: 'rs0', members: [ { _id: 0, host: '127.0.0.1:27017' } ] })Finally, on UNIX-based system (for example, Linux or Mac OS X) you may need to adjust the ulimit.
In case you need to, you can adjust the ulimit with the following command (32768 is just a recommendation):
$ ulimit -n 32768You can use ulimit -a again to verify the ulimit for "open files" was set appropriately.
$ ./mvnw clean install