Skip to content

Commit a36e292

Browse files
Update aggregation StringOperators documentation.
Update reference and java documentation. Add issue reference to tests. Align method names and format code. Original Pull Request: #3861
1 parent 494c22b commit a36e292

File tree

4 files changed

+113
-44
lines changed

4 files changed

+113
-44
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/StringOperators.java

+104-32
Original file line numberDiff line numberDiff line change
@@ -686,28 +686,70 @@ public RegexMatch regexMatch(String regex, String options) {
686686
private RegexMatch createRegexMatch() {
687687
return usesFieldRef() ? RegexMatch.valueOf(fieldReference) : RegexMatch.valueOf(expression);
688688
}
689-
690-
public ReplaceOne replaceOne(String find,String replacement) {
691-
return createReplaceOne().find(find).replacement(replacement);
689+
690+
/**
691+
* Creates new {@link AggregationExpression} that takes the associated string representation and replaces the first
692+
* occurrence of the search string with the given replacement.
693+
*
694+
* @param search
695+
* @param replacement
696+
* @return new instance of {@link ReplaceOne}.
697+
* @since 3.4
698+
*/
699+
public ReplaceOne replaceOne(String search, String replacement) {
700+
return createReplaceOne().find(search).replacement(replacement);
692701
}
693-
702+
703+
/**
704+
* Creates new {@link AggregationExpression} that takes the associated string representation and replaces the first
705+
* occurrence of the search string computed by the given {@link AggregationExpression} with the given replacement.
706+
*
707+
* @param search
708+
* @param replacement
709+
* @return new instance of {@link ReplaceOne}.
710+
* @since 3.4
711+
*/
712+
public ReplaceOne replaceOne(AggregationExpression search, String replacement) {
713+
return createReplaceOne().findValueOf(search).replacement(replacement);
714+
}
715+
694716
private ReplaceOne createReplaceOne() {
695717
return usesFieldRef() ? ReplaceOne.valueOf(fieldReference) : ReplaceOne.valueOf(expression);
696718
}
697-
698-
public ReplaceAll replaceAll(String find,String replacement) {
699-
return createReplaceAll().find(find).replacement(replacement);
719+
720+
/**
721+
* Creates new {@link AggregationExpression} that takes the associated string representation and replaces all
722+
* occurrences of the search string with the given replacement.
723+
*
724+
* @param search
725+
* @param replacement
726+
* @return new instance of {@link ReplaceOne}.
727+
* @since 3.4
728+
*/
729+
public ReplaceAll replaceAll(String search, String replacement) {
730+
return createReplaceAll().find(search).replacement(replacement);
731+
}
732+
733+
/**
734+
* Creates new {@link AggregationExpression} that takes the associated string representation and replaces all
735+
* occurrences of the search string computed by the given {@link AggregationExpression} with the given replacement.
736+
*
737+
* @param search
738+
* @param replacement
739+
* @return new instance of {@link ReplaceOne}.
740+
* @since 3.4
741+
*/
742+
public ReplaceAll replaceAll(AggregationExpression search, String replacement) {
743+
return createReplaceAll().findValueOf(search).replacement(replacement);
700744
}
701-
745+
702746
private ReplaceAll createReplaceAll() {
703747
return usesFieldRef() ? ReplaceAll.valueOf(fieldReference) : ReplaceAll.valueOf(expression);
704748
}
705749

706750
private boolean usesFieldRef() {
707751
return fieldReference != null;
708752
}
709-
710-
711753
}
712754

713755
/**
@@ -2096,18 +2138,35 @@ protected String getMongoMethod() {
20962138
return "$regexMatch";
20972139
}
20982140
}
2099-
2141+
21002142
/**
21012143
* {@link AggregationExpression} for {@code $replaceOne} which replaces the first instance of a search string in an
21022144
* input string with a replacement string. <br />
21032145
* <strong>NOTE:</strong> Requires MongoDB 4.4 or later.
2146+
*
2147+
* @author Divya Srivastava
2148+
* @author Christoph Strobl
2149+
* @since 3.4
21042150
*/
21052151
public static class ReplaceOne extends AbstractAggregationExpression {
21062152

21072153
protected ReplaceOne(Object value) {
21082154
super(value);
21092155
}
21102156

2157+
/**
2158+
* Creates new {@link ReplaceOne} using the given as {@literal input}.
2159+
*
2160+
* @param value must not be {@literal null}.
2161+
* @return new instance of {@link ReplaceOne}.
2162+
*/
2163+
public static ReplaceOne value(String value) {
2164+
2165+
Assert.notNull(value, "Value must not be null!");
2166+
2167+
return new ReplaceOne(Collections.singletonMap("input", value));
2168+
}
2169+
21112170
/**
21122171
* Creates new {@link ReplaceOne} using the value of the provided {@link Field fieldReference} as {@literal input}
21132172
* value.
@@ -2180,25 +2239,23 @@ public ReplaceOne replacementOf(AggregationExpression expression) {
21802239
/**
21812240
* The string to search for within the given input field.
21822241
*
2183-
* @param find must not be {@literal null}.
2242+
* @param value must not be {@literal null}.
21842243
* @return new instance of {@link ReplaceOne}.
21852244
*/
2186-
public ReplaceOne find(String searchStr) {
2245+
public ReplaceOne find(String value) {
21872246

2188-
Assert.notNull(searchStr, "Search string must not be null!");
2247+
Assert.notNull(value, "Search string must not be null!");
21892248

2190-
Map<String, Object> search = append("find", searchStr);
2191-
2192-
return new ReplaceOne(search);
2249+
return new ReplaceOne(append("find", value));
21932250
}
21942251

21952252
/**
21962253
* Specify the reference to the {@link Field field} holding the string to search for within the given input field.
21972254
*
2198-
* @param find must not be {@literal null}.
2255+
* @param fieldReference must not be {@literal null}.
21992256
* @return new instance of {@link ReplaceOne}.
22002257
*/
2201-
public ReplaceOne findOf(String fieldReference) {
2258+
public ReplaceOne findValueOf(String fieldReference) {
22022259

22032260
Assert.notNull(fieldReference, "fieldReference must not be null!");
22042261

@@ -2212,7 +2269,7 @@ public ReplaceOne findOf(String fieldReference) {
22122269
* @param expression must not be {@literal null}.
22132270
* @return new instance of {@link ReplaceOne}.
22142271
*/
2215-
public ReplaceOne findOf(AggregationExpression expression) {
2272+
public ReplaceOne findValueOf(AggregationExpression expression) {
22162273

22172274
Assert.notNull(expression, "Expression must not be null!");
22182275

@@ -2224,18 +2281,35 @@ protected String getMongoMethod() {
22242281
return "$replaceOne";
22252282
}
22262283
}
2227-
2284+
22282285
/**
22292286
* {@link AggregationExpression} for {@code $replaceAll} which replaces all instances of a search string in an input
22302287
* string with a replacement string. <br />
22312288
* <strong>NOTE:</strong> Requires MongoDB 4.4 or later.
2289+
*
2290+
* @author Divya Srivastava
2291+
* @author Christoph Strobl
2292+
* @since 3.4
22322293
*/
22332294
public static class ReplaceAll extends AbstractAggregationExpression {
22342295

22352296
protected ReplaceAll(Object value) {
22362297
super(value);
22372298
}
22382299

2300+
/**
2301+
* Creates new {@link ReplaceAll} using the given as {@literal input}.
2302+
*
2303+
* @param value must not be {@literal null}.
2304+
* @return new instance of {@link ReplaceOne}.
2305+
*/
2306+
public static ReplaceAll value(String value) {
2307+
2308+
Assert.notNull(value, "Value must not be null!");
2309+
2310+
return new ReplaceAll(Collections.singletonMap("input", value));
2311+
}
2312+
22392313
/**
22402314
* Creates new {@link ReplaceAll} using the value of the provided {@link Field fieldReference} as {@literal input}
22412315
* value.
@@ -2284,7 +2358,7 @@ public ReplaceAll replacement(String replacement) {
22842358
* @param fieldReference must not be {@literal null}.
22852359
* @return new instance of {@link ReplaceAll}.
22862360
*/
2287-
public ReplaceAll replacementOf(String fieldReference) {
2361+
public ReplaceAll replacementValueOf(String fieldReference) {
22882362

22892363
Assert.notNull(fieldReference, "FieldReference must not be null!");
22902364

@@ -2298,7 +2372,7 @@ public ReplaceAll replacementOf(String fieldReference) {
22982372
* @param expression must not be {@literal null}.
22992373
* @return new instance of {@link ReplaceAll}.
23002374
*/
2301-
public ReplaceAll replacementOf(AggregationExpression expression) {
2375+
public ReplaceAll replacementValueOf(AggregationExpression expression) {
23022376

23032377
Assert.notNull(expression, "Expression must not be null!");
23042378

@@ -2308,25 +2382,23 @@ public ReplaceAll replacementOf(AggregationExpression expression) {
23082382
/**
23092383
* The string to search for within the given input field.
23102384
*
2311-
* @param find must not be {@literal null}.
2385+
* @param value must not be {@literal null}.
23122386
* @return new instance of {@link ReplaceAll}.
23132387
*/
2314-
public ReplaceAll find(String searchStr) {
2315-
2316-
Assert.notNull(searchStr, "Search string must not be null!");
2388+
public ReplaceAll find(String value) {
23172389

2318-
Map<String, Object> search = append("find", searchStr);
2390+
Assert.notNull(value, "Search string must not be null!");
23192391

2320-
return new ReplaceAll(search);
2392+
return new ReplaceAll(append("find", value));
23212393
}
23222394

23232395
/**
23242396
* Specify the reference to the {@link Field field} holding the string to search for within the given input field.
23252397
*
2326-
* @param find must not be {@literal null}.
2398+
* @param fieldReference must not be {@literal null}.
23272399
* @return new instance of {@link ReplaceAll}.
23282400
*/
2329-
public ReplaceAll findOf(String fieldReference) {
2401+
public ReplaceAll findValueOf(String fieldReference) {
23302402

23312403
Assert.notNull(fieldReference, "fieldReference must not be null!");
23322404

@@ -2339,7 +2411,7 @@ public ReplaceAll findOf(String fieldReference) {
23392411
* @param expression must not be {@literal null}.
23402412
* @return new instance of {@link ReplaceAll}.
23412413
*/
2342-
public ReplaceAll findOf(AggregationExpression expression) {
2414+
public ReplaceAll findValueOf(AggregationExpression expression) {
23432415

23442416
Assert.notNull(expression, "Expression must not be null!");
23452417

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/SpelExpressionTransformerUnitTests.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -865,17 +865,17 @@ void shouldRenderRegexMatchWithOptionsFromFieldReference() {
865865
.isEqualTo("{ \"$regexMatch\" : {\"input\" : \"$field1\" , \"regex\" : \"e\" , \"options\" : \"$field2\"}}");
866866
}
867867

868-
@Test
868+
@Test // GH-3695
869869
void shouldRenderReplaceOne() {
870870

871-
assertThat(transform("replaceOne(field,'bar','baz')"))
871+
assertThat(transform("replaceOne(field, 'bar', 'baz')"))
872872
.isEqualTo("{ \"$replaceOne\" : {\"input\" : \"$field\" , \"find\" : \"bar\" , \"replacement\" : \"baz\"}}");
873873
}
874874

875-
@Test
875+
@Test // GH-3695
876876
void shouldRenderReplaceAll() {
877877

878-
assertThat(transform("replaceAll(field,'bar','baz')"))
878+
assertThat(transform("replaceAll(field, 'bar', 'baz')"))
879879
.isEqualTo("{ \"$replaceAll\" : {\"input\" : \"$field\" , \"find\" : \"bar\" , \"replacement\" : \"baz\"}}");
880880
}
881881

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/StringOperatorsUnitTests.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -282,34 +282,31 @@ void shouldRenderRegexFindWithOptionsExpression() {
282282
+ " } } ");
283283
}
284284

285-
@Test
285+
@Test // GH-3695
286286
void shouldRenderReplaceOne() {
287287

288288
assertThat(StringOperators.valueOf("bar").replaceOne("foobar","baz").toDocument(Aggregation.DEFAULT_CONTEXT))
289289
.isEqualTo("{ $replaceOne : {\"find\" : \"foobar\", \"input\" : \"$bar\", \"replacement\" : \"baz\"}}");
290290
}
291291

292-
@Test
292+
@Test // GH-3695
293293
void shouldRenderReplaceOneForExpression() {
294294

295295
assertThat(StringOperators.valueOf(EXPRESSION).replaceOne("a","s").toDocument(Aggregation.DEFAULT_CONTEXT))
296296
.isEqualTo("{ $replaceOne : {\"find\" : \"a\", \"input\" : " + EXPRESSION_STRING + ", \"replacement\" : \"s\"}}");
297297
}
298298

299-
@Test
299+
@Test // GH-3695
300300
void shouldRenderReplaceAll() {
301301

302302
assertThat(StringOperators.valueOf("bar").replaceAll("foobar","baz").toDocument(Aggregation.DEFAULT_CONTEXT))
303303
.isEqualTo("{ $replaceAll : {\"find\" : \"foobar\", \"input\" : \"$bar\", \"replacement\" : \"baz\"}}");
304304
}
305305

306-
@Test
306+
@Test // GH-3695
307307
void shouldRenderReplaceAllForExpression() {
308308

309309
assertThat(StringOperators.valueOf(EXPRESSION).replaceAll("a","s").toDocument(Aggregation.DEFAULT_CONTEXT))
310310
.isEqualTo("{ $replaceAll : {\"find\" : \"a\", \"input\" : " + EXPRESSION_STRING + ", \"replacement\" : \"s\"}}");
311311
}
312-
313-
314-
315312
}

src/main/asciidoc/reference/aggregation-framework.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ At the time of this writing, we provide support for the following Aggregation Op
8888
| `abs`, `acos`, `acosh`, `add` (+++*+++ via `plus`), `asin`, `asin`, `atan`, `atan2`, `atanh`, `ceil`, `cos`, `cosh`, `derivative`, `divide`, `exp`, `floor`, `integral`, `ln`, `log`, `log10`, `mod`, `multiply`, `pow`, `round`, `sqrt`, `subtract` (+++*+++ via `minus`), `sin`, `sinh`, `tan`, `tanh`, `trunc`
8989

9090
| String Aggregation Operators
91-
| `concat`, `substr`, `toLower`, `toUpper`, `strcasecmp`, `indexOfBytes`, `indexOfCP`, `regexFind`, `regexFindAll`, `regexMatch`, `split`, `strLenBytes`, `strLenCP`, `substrCP`, `trim`, `ltrim`, `rtim`
91+
| `concat`, `substr`, `toLower`, `toUpper`, `strcasecmp`, `indexOfBytes`, `indexOfCP`, `regexFind`, `regexFindAll`, `regexMatch`, `replaceAll`, `replaceOne`, split`, `strLenBytes`, `strLenCP`, `substrCP`, `trim`, `ltrim`, `rtim`
9292

9393
| Comparison Aggregation Operators
9494
| `eq` (+++*+++ via `is`), `gt`, `gte`, `lt`, `lte`, `ne`

0 commit comments

Comments
 (0)