Skip to content

Commit 2f713be

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-1424 - Add support for NOT_LIKE.
We now support `notLike` and `isNotLike` in query derivation. Original pull request: #364.
1 parent e03520d commit 2f713be

File tree

6 files changed

+85
-4
lines changed

6 files changed

+85
-4
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/MongoRegexCreator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ private String prepareAndEscapeStringBeforeApplyingLikeRegex(String source, Type
7272
return source;
7373
}
7474

75-
if (!ObjectUtils.nullSafeEquals(Type.LIKE, type)) {
75+
if (!ObjectUtils.nullSafeEquals(Type.LIKE, type) && !ObjectUtils.nullSafeEquals(Type.NOT_LIKE, type)) {
7676
return PUNCTATION_PATTERN.matcher(source).find() ? Pattern.quote(source) : source;
7777
}
7878

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoQueryCreator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2015 the original author or authors.
2+
* Copyright 2010-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -199,6 +199,8 @@ private Criteria from(Part part, MongoPersistentProperty property, Criteria crit
199199
case ENDING_WITH:
200200
case CONTAINING:
201201
return createContainingCriteria(part, property, criteria, parameters);
202+
case NOT_LIKE:
203+
return createContainingCriteria(part, property, criteria.not(), parameters);
202204
case NOT_CONTAINING:
203205
return createContainingCriteria(part, property, criteria.not(), parameters);
204206
case REGEX:

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,4 +1296,15 @@ public void findBySkillsNotContains() throws Exception {
12961296
assertThat(result, not(hasItem(carter)));
12971297
}
12981298

1299+
/*
1300+
* @see DATAMONGO-1424
1301+
*/
1302+
@Test
1303+
public void findsPersonsByFirstnameNotLike() throws Exception {
1304+
1305+
List<Person> result = repository.findByFirstnameNotLike("Bo*");
1306+
assertThat(result.size(), is((int) (repository.count() - 1)));
1307+
assertThat(result, not(hasItem(boyd)));
1308+
}
1309+
12991310
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepository.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2015 the original author or authors.
2+
* Copyright 2010-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -91,6 +91,14 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
9191

9292
List<Person> findByFirstnameNotContains(String firstname);
9393

94+
/**
95+
* Returns all {@link Person}s with a firstname not matching the given one (*-wildcard supported).
96+
*
97+
* @param firstname
98+
* @return
99+
*/
100+
List<Person> findByFirstnameNotLike(String firstname);
101+
94102
List<Person> findByFirstnameLikeOrderByLastnameAsc(String firstname, Sort sort);
95103

96104
List<Person> findBySkillsContains(List<String> skills);

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryCreatorUnitTests.java

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2015 the original author or authors.
2+
* Copyright 2011-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -669,6 +669,62 @@ public void bindsNullValueToContainsClause() {
669669
assertThat(query, is(query(where("emailAddresses").in((Object) null))));
670670
}
671671

672+
/**
673+
* @see DATAMONGO-1424
674+
*/
675+
@Test
676+
public void notLikeShouldEscapeSourceWhenUsedWithLeadingAndTrailingWildcard() {
677+
678+
PartTree tree = new PartTree("findByUsernameNotLike", User.class);
679+
ConvertingParameterAccessor accessor = getAccessor(converter, "*fire.fight+*");
680+
681+
Query query = new MongoQueryCreator(tree, accessor, context).createQuery();
682+
683+
assertThat(query.getQueryObject(),
684+
is(query(where("username").not().regex(".*\\Qfire.fight+\\E.*")).getQueryObject()));
685+
}
686+
687+
/**
688+
* @see DATAMONGO-1424
689+
*/
690+
@Test
691+
public void notLikeShouldEscapeSourceWhenUsedWithLeadingWildcard() {
692+
693+
PartTree tree = new PartTree("findByUsernameNotLike", User.class);
694+
ConvertingParameterAccessor accessor = getAccessor(converter, "*steel.heart+");
695+
696+
Query query = new MongoQueryCreator(tree, accessor, context).createQuery();
697+
698+
assertThat(query.getQueryObject(),
699+
is(query(where("username").not().regex(".*\\Qsteel.heart+\\E")).getQueryObject()));
700+
}
701+
702+
/**
703+
* @see DATAMONGO-1424
704+
*/
705+
@Test
706+
public void notLikeShouldEscapeSourceWhenUsedWithTrailingWildcard() {
707+
708+
PartTree tree = new PartTree("findByUsernameNotLike", User.class);
709+
MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, "cala.mity+*"), context);
710+
Query query = creator.createQuery();
711+
712+
assertThat(query.getQueryObject(), is(query(where("username").not().regex("\\Qcala.mity+\\E.*")).getQueryObject()));
713+
}
714+
715+
/**
716+
* @see DATAMONGO-1424
717+
*/
718+
@Test
719+
public void notLikeShouldBeTreatedCorrectlyWhenUsedWithWildcardOnly() {
720+
721+
PartTree tree = new PartTree("findByUsernameNotLike", User.class);
722+
ConvertingParameterAccessor accessor = getAccessor(converter, "*");
723+
724+
Query query = new MongoQueryCreator(tree, accessor, context).createQuery();
725+
assertThat(query.getQueryObject(), is(query(where("username").not().regex(".*")).getQueryObject()));
726+
}
727+
672728
interface PersonRepository extends Repository<Person, Long> {
673729

674730
List<Person> findByLocationNearAndFirstname(Point location, Distance maxDistance, String firstname);

src/main/asciidoc/reference/mongo-repositories.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ NOTE: Note that for version 1.0 we currently don't support referring to paramete
208208
| `findByFirstnameLike(String name)`
209209
| `{"firstname" : name} ( name as regex)`
210210

211+
| `NotLike`, `IsNotLike`, `EndingWith`
212+
| `findByFirstnameNotLike(String name)`
213+
| `{"firstname" : { "$not" : name }} ( name as regex)`
214+
211215
| `Containing` on String
212216
| `findByFirstnameContaining(String name)`
213217
| `{"firstname" : name} (name as regex)`

0 commit comments

Comments
 (0)