Giter Site home page Giter Site logo

spring-projects / spring-data-mongodb Goto Github PK

View Code? Open in Web Editor NEW
1.6K 169.0 1.1K 33.19 MB

Provides support to increase developer productivity in Java when using MongoDB. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.

Home Page: https://spring.io/projects/spring-data-mongodb/

License: Apache License 2.0

Java 98.12% JavaScript 0.01% Kotlin 1.79% Dockerfile 0.08%
ddd java mongodb spring spring-data framework

spring-data-mongodb's Introduction

Spring Data MongoDB Spring Data MongoDB

Spring Data MongoDB icon?job=spring data mongodb%2Fmain&subject=Build Gitter Revved up by Develocity

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 MongoDB 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.

Code of Conduct

This project is governed by the Spring Code of Conduct. By participating, you are expected to uphold this code of conduct. Please report unacceptable behavior to [email protected].

Getting Started

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("Gierke");
    repository.save(person);

    List<Person> lastNameResults = repository.findByLastname("Gierke");
    List<Person> firstNameResults = repository.findByFirstnameLike("Oli*");
 }
}

@Configuration
@EnableMongoRepositories
class ApplicationConfig extends AbstractMongoClientConfiguration {

  @Override
  protected String getDatabaseName() {
    return "springdata";
  }
}

Maven configuration

Add the Maven dependency:

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-mongodb</artifactId>
  <version>${version}</version>
</dependency>

If you’d rather like the latest snapshots of the upcoming major version, use our Maven snapshot repository and declare the appropriate dependency version.

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-mongodb</artifactId>
  <version>${version}-SNAPSHOT</version>
</dependency>

<repository>
  <id>spring-snapshot</id>
  <name>Spring Snapshot Repository</name>
  <url>https://repo.spring.io/snapshot</url>
</repository>

Upgrading

Instructions for how to upgrade from earlier versions of Spring Data are provided on the project wiki. Follow the links in the release notes section to find the version that you want to upgrade to.

Getting Help

Having trouble with Spring Data? We’d love to help!

Reporting Issues

Spring Data uses Github as issue tracking system to record bugs and feature requests. If you want to raise an issue, please follow the recommendations below:

  • Before you log a bug, please search the issue tracker to see if someone has already reported the problem.

  • If the issue does not already exist, create a new issue.

  • Please provide as much information as possible with the issue report, we like to know the version of Spring Data that you are using, the JVM version, Stacktrace, etc.

  • If you need to paste code, or include a stack trace use Markdown code fences ```.

Guides

The spring.io site contains several guides that show how to use Spring Data step-by-step:

Examples

Building from Source

You do not need to build from source to use Spring Data. Binaries are available in repo.spring.io and accessible from Maven using the Maven configuration noted above.

Note
Configuration for Gradle is similar to Maven.

The best way to get started is by creating a Spring Boot project using MongoDB on start.spring.io. Follow this link to build an imperative application and this link to build a reactive one.

However, if you want to try out the latest and greatest, Spring Data MongoDB can be easily built with the Maven wrapper and minimally, JDK 17 (JDK downloads).

In order to build Spring Data MongoDB, you will need to download and install a MongoDB distribution.

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: 6.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 32768

You can use ulimit -a again to verify the ulimit for "open files" was set appropriately.

Now you are ready to build Spring Data MongoDB. Simply enter the following mvnw (Maven Wrapper) command:

 $ ./mvnw clean install

If you want to build with the regular mvn command, you will need Maven v3.8.0 or above.

Also see CONTRIBUTING.adoc if you wish to submit pull requests, and in particular, please sign the Contributor’s Agreement before your first non-trivial change.

Building reference documentation

Building the documentation builds also the project without running tests.

 $ ./mvnw clean install -Pantora

The generated documentation is available from target/antora/site/index.html.

License

Spring Data MongoDB is Open Source software released under the Apache 2.0 license.

spring-data-mongodb's People

Contributors

a-zink avatar ahli avatar baumgartner avatar christophstrobl avatar divyajnu08 avatar erichaagdev avatar francoiskha avatar graemerocher avatar gregturn avatar gustavodegeus avatar hlang avatar jbrisbin avatar jxblum avatar kioyong avatar linkedlist avatar markpollack avatar michael-simons avatar mp911de avatar noter avatar odrotbohm avatar petitcl avatar phil-schneider avatar rolag-it avatar sangyongchoi avatar schauder avatar sdeleuze avatar spring-builds avatar sxhinzvc avatar yacota avatar ydemartino avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

spring-data-mongodb's Issues

Sample application(s) [DATAMONGO-29]

Mark Pollack opened DATAMONGO-29 and commented

Sample applications sit in a separate repository but should be sync'd with the M1 release.
Two types of examples are important to show

  1. Generic Repository data access (CRUD) on some domain object
  2. Specific features of MongoTemplate that highlight typical MongoDB use cases, such as partial update.

Existing apps show

  1. Spring MVC analytics
  2. Collection of file system information

Could look into music play-list style application or use of Restaurants domain from Chris Richardson's POJOs in action as much has been done in neo and couchdb to support that domain


No further details from DATAMONGO-29

Rich object mapping support [DATAMONGO-50]

Jon Brisbin opened DATAMONGO-50 and commented

We should incorporate code from the spring-data-mapping project (formerly Grails' inconsequential) to support rich mapping in the MongoDB support.

Things we'll need to address include:

  • Implementing the right MappingContext and MappingConfigurationStrategy objects to read an object's annotations and configure the mapper appropriately.
  • Saving and reading child collections in case of relationships.
  • Implementing a special MongoConverter class to handle reading and writing objects

Affects: 1.0 M1

Sub-tasks:

SimpleMongoConverter could support identifying Spring EL expressions in keys [DATAMONGO-32]

Oliver Drotbohm opened DATAMONGO-32 and commented

Besides plain JSON array hierarchies the SimpleMongoConverter could support Spring EL expressions for keys such as:

{"parameters.p1" : "1" , "count" : 5.0}

Need to discuss, how to detect Spring EL in this case. We could simply forbid dots in plain keys and treat every key containing a dot as Spring EL or enforce concrete EL delimter usage #{...}


No further details from DATAMONGO-32

Support for map-reduce operations in MongoTemplate [DATAMONGO-7]

Mark Pollack opened DATAMONGO-7 and commented

The use of MapReduce via the driver is cumbersome, provide a layer that exposes map reduce operations with type-safe helper objects.

Investigate: http://cookbook.mongodb.org/ lists a few common cases where one would use map-reduce, typically to sort/count document array elements. See how this might be better packaged up so as to avoid Java users from having to write JavaScript/MapReduce for common tasks


Issue Links:

  • DATAMONGO-182 add 'group' and 'mapreduce' to the MongoOps
    ("Cloned from")

1 votes, 1 watchers

Validation support to MongoTemplate [DATAMONGO-36]

Mark Pollack opened DATAMONGO-36 and commented

Spring 3.0 supports JSR-303. If

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
is defined, we can ask the container for the validator (is there an Aware interface?) and then perform validation within MongoTemplate methods that take a POJO for Create, update, or delete operations.


Affects: 1.0 M2

Attachments:

Referenced from: commits 5b13307

1 votes, 3 watchers

Extract MongoOperations from MongoTemplate [DATAMONGO-25]

Oliver Drotbohm opened DATAMONGO-25 and commented

We should introduce a MongoOperations interface for the functionality provided in MongoTemplate to decrease the coupling of potential clients (e.g. the repository abstraction) and align to all other Spring templates that follow that guideline.

It's probably a good idea to do this while polishing the JavaDoc as well


Affects: 1.0 M1

Issue Links:

  • DATAMONGO-18 Extract common MongoTemplate methods into MongoOperations interface
    ("duplicates")

Add support for specifying attributes to entities, e.g., collection name, field names, etc. [DATAMONGO-40]

Oruganti Shanker opened DATAMONGO-40 and commented

Need to be able to specify the collection name for each repository. The collection name used for each entity should be specifiable as an annotation, e.g., the Table annotation.

Another important enabler for using spring-data-mongo in production code is to add a few annotations which have similar functionality to the @Reference, @Embedded etc. in Morphia, and for naming the fieldnames in the persistence store differently from the fieldnames used in the Java Entity. Of course these annotations should be consistent with JPA annotations, and should be a superset of JPA annotations.

See the thread http://forum.springsource.org/showthread.php?t=102127 in the Forum


Attachments:

1 votes, 2 watchers

Id property is overridden by Spring Data's own Id property [DATAMONGO-44]

Mark Serrano opened DATAMONGO-44 and commented

When creating a simple POJO with an id property and persisting this object to MongoDB, the id property is overridden by Spring Data. It merges the "id" with "_id".

For example:
{ "_id" : "69133244-fcc0-42b2-aa59-308f00c75860", "firstName" : "John", "money" : 1000, "lastName" : "Smith" }

{ "_id" : "45d236ff-6a38-4767-a973-6f3d70ff2448", "firstName" : "Jane", "money" : 2000, "lastName" : "Adams" }

{ "_id" : "9dcba269-9ed5-45c6-887e-a2de8fec334d", "firstName" : "Jeff", "money" : 3000, "lastName" : "Mayer" }

The native MongoDB preserves both the "id" and "_id", while Spring Data combines both as "_id". As a workaround, I have to rename my POJO's id property as "pid" (any other name will work).

For example:
{ "_id" : ObjectId("4d5e7b3bfdece02281f253a4"), "id" : "f1e6b8b2-9d68-495c-9d6e-b81cc35cf9bc", "firstName" : "John", "lastName" : "Smith", "money" : 1000 }

{ "_id" : ObjectId("4d5e7b3bfdece02282f253a4"), "id" : "39f6ebfb-10cf-4702-a5b8-8bc2489cebcc", "firstName" : "Jane", "lastName" : "Adams", "money" : 2000 }

{ "_id" : ObjectId("4d5e7b3bfdece02283f253a4"), "id" : "f4932004-c320-47e4-b4b3-7d07f3aaabb9", "firstName" : "Jeff", "lastName" : "Mayer", "money" : 3000

See the following tutorial that shows this "pid" workaround: http://krams915.blogspot.com/2011/02/spring-data-mongodb-tutorial.html

See the following tutorial that uses native MongoDB which correctly preserves both "id" and "_id": http://krams915.blogspot.com/2011/01/spring-mvc-3-using-document-oriented.html


Reference URL: http://forum.springsource.org/showthread.php?p=346693&posted=1#post346693

Issue Links:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.