How to I get Spring-Data-MongoDB to validate my objects?

First make sure that you have JSR-303 validator on classpath, for example: <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>4.2.0.Final</version> </dependency> If you use Java config, the way to go is to create 2 beans: @Bean public ValidatingMongoEventListener validatingMongoEventListener() { return new ValidatingMongoEventListener(validator()); } @Bean public LocalValidatorFactoryBean validator() { return new LocalValidatorFactoryBean(); } Voilà! Validation is working now.

Create filter aggregation in spring

You can try below query. Static Imports import static org.springframework.data.mongodb.core.aggregation.Aggregation.*; import static org.springframework.data.mongodb.core.aggregation.ArrayOperators.Filter.filter; import static org.springframework.data.mongodb.core.aggregation.ComparisonOperators.Eq.valueOf; Code Aggregation aggregation = newAggregation( project().and(filter(“parts”) .as(“item”) .by(valueOf( “item.currentState”) .equalToValue( “Estimation Confirmed”))) .as(“parts”); ); List<outputType> results = mongoTemplate.aggregate(aggregation, inputType, outputType)

Mongo unique index case insensitive

Prior of MongoDB version 3.4 we were unable to create index with case insensitive. In version 3.4 has collation option that allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks. The collation option has the following syntax: collation: { locale: <string>, caseLevel: <boolean>, caseFirst: <string>, strength: <int>, … Read more

How To Configure MongoDb Collection Name For a Class in Spring Data

The only way you can currently achieve this is by annotating your domain class with @Document using the collection property to define the name of the collection instances of this class shall be persisted to. However, there’s a JIRA issue open that suggests adding a pluggable naming strategy to configure the ways class, collection and … Read more

Configure Multiple MongoDB repositories with Spring Data Mongo

The base idea is to separate the package hierarchy that contains your repositories into two different paths: com.whatever.repositories.main package for the main db repository interfaces com.whatever.repositories.secondary package for the other db repository interfaces Your XML configuration should be something such as: <mongo:repositories base-package=”com.whatever.repositories.main” mongo-template-ref=”mongoTemplate”/> <mongo:repositories base-package=”com.whatever.repositories.secondary” mongo-template-ref=”mongoAppTemplate”/> EDIT @EnableMongoRepositories annotation is not @Repeatable, but you … Read more

Set MongoDb converter programmatically

This answer may be a little late for the OP, but I just ran into the same problem today and found a solution… To set it up programmatically, you need to call MongoMappingConverter.afterPropertiesSet() before you use it. I realized this from reading the code for MongoTemplate.getDefaultMongoConverter(MongoDbFactory). Here’s an example: MappingMongoConverter converter = new MappingMongoConverter(mongoDbFactory, context); … Read more