Skip to content

DATAMONGO-1424 - Add support for NOT_LIKE. #364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-1424-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-mongodb-cross-store/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-1424-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-1424-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-1424-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-log4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-1424-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-1424-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private String prepareAndEscapeStringBeforeApplyingLikeRegex(String source, Type
return source;
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 the original author or authors.
* Copyright 2010-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -199,6 +199,8 @@ private Criteria from(Part part, MongoPersistentProperty property, Criteria crit
case ENDING_WITH:
case CONTAINING:
return createContainingCriteria(part, property, criteria, parameters);
case NOT_LIKE:
return createContainingCriteria(part, property, criteria.not(), parameters);
case NOT_CONTAINING:
return createContainingCriteria(part, property, criteria, parameters).not();
case REGEX:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1261,4 +1261,14 @@ public void findAllByExampleShouldResolveStuffCorrectly() {
assertThat(result.size(), is(2));
}

/**
* @see DATAMONGO-1424
*/
@Test
public void findsPersonsByFirstnameNotLike() throws Exception {

List<Person> result = repository.findByFirstnameNotLike("Bo*");
assertThat(result.size(), is((int) (repository.count() - 1)));
assertThat(result, not(hasItem(boyd)));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 the original author or authors.
* Copyright 2010-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -89,6 +89,14 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
*/
List<Person> findByFirstnameLike(String firstname);

/**
* Returns all {@link Person}s with a firstname not matching the given one (*-wildcard supported).
*
* @param firstname
* @return
*/
List<Person> findByFirstnameNotLike(String firstname);

List<Person> findByFirstnameLikeOrderByLastnameAsc(String firstname, Sort sort);

@Query("{'age' : { '$lt' : ?0 } }")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2015 the original author or authors.
* Copyright 2011-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -668,6 +668,62 @@ public void bindsNullValueToContainsClause() {
assertThat(query, is(query(where("emailAddresses").in((Object) null))));
}

/**
* @see DATAMONGO-1424
*/
@Test
public void notLikeShouldEscapeSourceWhenUsedWithLeadingAndTrailingWildcard() {

PartTree tree = new PartTree("findByUsernameNotLike", User.class);
ConvertingParameterAccessor accessor = getAccessor(converter, "*fire.fight+*");

Query query = new MongoQueryCreator(tree, accessor, context).createQuery();

assertThat(query.getQueryObject(),
is(query(where("username").not().regex(".*\\Qfire.fight+\\E.*")).getQueryObject()));
}

/**
* @see DATAMONGO-1424
*/
@Test
public void notLikeShouldEscapeSourceWhenUsedWithLeadingWildcard() {

PartTree tree = new PartTree("findByUsernameNotLike", User.class);
ConvertingParameterAccessor accessor = getAccessor(converter, "*steel.heart+");

Query query = new MongoQueryCreator(tree, accessor, context).createQuery();

assertThat(query.getQueryObject(),
is(query(where("username").not().regex(".*\\Qsteel.heart+\\E")).getQueryObject()));
}

/**
* @see DATAMONGO-1424
*/
@Test
public void notLikeShouldEscapeSourceWhenUsedWithTrailingWildcard() {

PartTree tree = new PartTree("findByUsernameNotLike", User.class);
MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, "cala.mity+*"), context);
Query query = creator.createQuery();

assertThat(query.getQueryObject(), is(query(where("username").not().regex("\\Qcala.mity+\\E.*")).getQueryObject()));
}

/**
* @see DATAMONGO-1424
*/
@Test
public void notLikeShouldBeTreatedCorrectlyWhenUsedWithWildcardOnly() {

PartTree tree = new PartTree("findByUsernameNotLike", User.class);
ConvertingParameterAccessor accessor = getAccessor(converter, "*");

Query query = new MongoQueryCreator(tree, accessor, context).createQuery();
assertThat(query.getQueryObject(), is(query(where("username").not().regex(".*")).getQueryObject()));
}

interface PersonRepository extends Repository<Person, Long> {

List<Person> findByLocationNearAndFirstname(Point location, Distance maxDistance, String firstname);
Expand Down
4 changes: 4 additions & 0 deletions src/main/asciidoc/reference/mongo-repositories.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ NOTE: Note that for version 1.0 we currently don't support referring to paramete
| `findByFirstnameLike(String name)`
| `{"firstname" : name} ( name as regex)`

| `NotLike`, `IsNotLike`, `EndingWith`
| `findByFirstnameNotLike(String name)`
| `{"firstname" : { "$not" : name }} ( name as regex)`

| `Containing` on String
| `findByFirstnameContaining(String name)`
| `{"firstname" : name} (name as regex)`
Expand Down