Questions tagged [spring-data-mongodb]

Spring Data for MongoDB is part of the Spring Data project which provides 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 MongoDB document database. Key functional areas of Spring Data MongoDB are a POJO centric model for interacting with a MongoDB Collection and easily writing a Repository style data access layer.

Spring Data for MongoDB is part of the umbrella Spring Data project which 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 DBCollection and easily writing a Repository style data access layer

Features

  • Spring configuration support using Java based @Configuration classes or an XML namespace for a Mongo driver instance and replica sets.
  • MongoTemplate and ReactiveMongoTemplate helper classes that increase productivity performing common Mongo operations.
  • Fluent Template API.
  • Support for MongoDB Transactions.
  • Listener containers for Change Streams and Tailable cursors.
  • Includes integrated object mapping between documents and POJOs.
  • Exception translation into Spring's portable Data Access Exception hierarchy.
  • Feature Rich Object Mapping integrated with Spring's Conversion Service.
  • Annotation based mapping metadata but extensible to support other metadata formats.
  • Persistence and mapping lifecycle events.
  • Low-level mapping using MongoReader/MongoWriter abstractions.
  • Java based Query, Criteria, and Update DSLs.
  • Automatic implementation of Repository interfaces including support for custom finder methods.
  • QueryDSL integration to support type-safe queries.
  • Cross-store persistence - support for JPA Entities with fields transparently persisted/retrieved using MongoDB.
  • GeoSpatial integration.
  • Map-Reduce integration.
  • JMX administration and monitoring.
  • CDI support for repositories.
  • GridFS support.

Online Resources:

Frequently Asked Questions

3281 questions
1
vote
0 answers

Spring-data-mongodb DbRef Lazy with @Version cause org.springframework.dao.DuplicateKeyException

Spring-boot 2.1.4, Spring-Data-Mongo 2.1.6, DuplicateKeyException when I load and re-save lazy loaded content. @Document public class User { @Id private String id; @Version private Long version; @DBRef(lazy = true) private Role…
lollito
  • 11
  • 4
1
vote
2 answers

Is the annotation @EnableMongoRepositories required while creating rest endpoints using Spring data rest @RepositoryRestResource

I was trying out the spring boot guide to create a spring data rest endpoint using the @RepositoryRestResource annotation. What i observed was that in the guide , they have not specified us to use any other annotations rather than…
1
vote
1 answer

Call "Optional#isPresent()" before accessing the value issue with Pageable

I am using Spring Data Mongo Pageable and Sonar giving me below error: Optional optional = pageable.getSort().stream().findFirst(); if(optional.isPresent()) { direction = pageable.getSort().stream().findFirst().get().getDirection(); …
PAA
  • 1
  • 46
  • 174
  • 282
1
vote
0 answers

The value for annotation attribute PageableDefault.size must be a constant expression

I already went through many links but did not find solution. Here is my code: @Value("${paging.default.pageSize}") public int pageSize; Controller method: @GetMapping() public ResponseEntity> getAllEmployee(@PageableDefault(size =…
PAA
  • 1
  • 46
  • 174
  • 282
1
vote
2 answers

How to ignore null or empty values while using find() in Spring MongoRepository

I have a search functionality where there are different parameters and user can choose one or multiple parameters and ignore other parameters. I want to use findByFirstNameAndLastNameAndAddressAndCountry() for this so that if any parameter is null…
Chandan Kumar
  • 313
  • 4
  • 9
1
vote
2 answers

How to specify the using mongo database in a MongoRepository when there are multiple databases

In my spring boot application i have configured two databases in my yml file. So now I want to specify which database is to use in each MongoRepository. Is this possible? If so, how to do it? Thanks in advance. yml file : spring: …
GeekySelene
  • 847
  • 3
  • 13
  • 31
1
vote
0 answers

How to do integration testing for custom mongo repository?

These are the following configuration that I have done in my project. @Configuration public class AppMongoConfig { @Autowired private MongoDbFactory mongoDbFactory; @Autowired private MongoMappingContext…
1
vote
0 answers

Write enum to DB as lowercase value

I am getting a @RequestBody where one of the fields is an enum in my model. ie public enum X{ VALUE("Value") } I am able to pass in "Value" with the request which gets mapped to the correct enum value just fine. But when I save the body to…
1
vote
1 answer

How to load mongo repository to spring test application context?

I have to implement some test for some spring application. I am using @SpringBootTest annotation in my test: @SpringBootTest @TestInstance(TestInstance.Lifecycle.PER_CLASS) class MyTest(){ //some tests... } It works fine, but i do not…
Stanislav Serdiuk
  • 421
  • 1
  • 6
  • 21
1
vote
1 answer

Spring Data Mongo: Find by array intersection

I use Spring Data DSL to query MongoDB in the following way: Page findByCommunitiesCodeContaining(String code, Pageable pageable); It returns all Members of a community with a given code. Problem: I need to pass a collection of community…
Sasha Shpota
  • 9,436
  • 14
  • 75
  • 148
1
vote
1 answer

MappingException (Ambiguous field mapping detected) with Spring Data on inherited field

The following results in a MappingException. Do I need to change my design? public class Foo extends Bar { // if class == Foo do not send this over the wire @JsonProperty(access = Access.WRITE_ONLY) public List
Codehai
  • 524
  • 1
  • 7
  • 27
1
vote
0 answers

Spring Data MongoDB Aggregation pipeline through annotation

Is there a way to define the aggregation pipeline "raw" string (with placeholders) in Spring Data the same way you can do it with @Query annotation on an method inside of a MongoRepository interface? If yes, can you please provide a simple…
user1902183
  • 3,203
  • 9
  • 31
  • 48
1
vote
2 answers

Caused by: org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class com.example.Hobbies

How to insert the embedded documents using BSONObject? When I am trying to insert the embedded document, getting error shown below. I wanted to save null values for the Date and firstName for example. I tried some options using Spring Data Mongo,…
user8937422
1
vote
2 answers

Spring Data Mongo doesn't persist field value as null - This doesn't works

I already went through the link: https://jira.spring.io/browse/DATAMONGO-1107 and also I dont see option to set spring.data.mongodb.persist-null-fields=true in application.properties file. Spring Data Mongo doesn't even persist a field having value…
user8937422
1
vote
1 answer

Spring Mongodb: Subtract two dates

I'm running against an issue related to spring data MongoDB. I need to subtract two dates: now - fieldDate. Nevertheless, it's come me up this problem with spring data MongoDB: I'm not able to subtract dates! I mean: As you can see, I'm only able…
Jordi
  • 20,868
  • 39
  • 149
  • 333