Skip to content

Commit 8c0fe2c

Browse files
committed
spring-projectsGH-3726: Add support for $sampleRate.
Closes spring-projects#3726
1 parent 4491301 commit 8c0fe2c

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java

+16
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
* @author Andreas Zink
6565
* @author Ziemowit Stolarczyk
6666
* @author Clément Petit
67+
* @author James McNee
6768
*/
6869
public class Criteria implements CriteriaDefinition {
6970

@@ -390,6 +391,21 @@ public Criteria exists(boolean value) {
390391
return this;
391392
}
392393

394+
/**
395+
* Creates a criterion using the {@literal $sampleRate} operator.
396+
*
397+
* @param sampleRate sample rate to determine number of documents to be randomly selected from the input.
398+
* @return this.
399+
* @see <a href="https://docs.mongodb.com/manual/reference/operator/aggregation/sampleRate/">MongoDB Query operator: $sampleRate</a>
400+
*/
401+
public Criteria sampleRate(double sampleRate) {
402+
Assert.isTrue(sampleRate >= 0, "The sample rate must be greater than zero!");
403+
Assert.isTrue(sampleRate <= 1, "The sample rate must not be greater than one!");
404+
405+
criteria.put("$sampleRate", sampleRate);
406+
return this;
407+
}
408+
393409
/**
394410
* Creates a criterion using the {@literal $type} operator.
395411
*

Diff for: spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/CriteriaUnitTests.java

+16
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,22 @@ public void shouldNegateFollowingSimpleExpression() {
156156
assertThat(co).isEqualTo(Document.parse("{ \"age\" : { \"$not\" : { \"$gt\" : 18}} , \"status\" : \"student\"}"));
157157
}
158158

159+
@Test // GH-3726
160+
public void shouldBuildCorrectSampleRateOperation() {
161+
Criteria c = new Criteria().sampleRate(0.4);
162+
assertThat(c.getCriteriaObject()).isEqualTo(Document.parse("{ \"$sampleRate\" : 0.4 }"));
163+
}
164+
165+
@Test // GH-3726
166+
public void shouldThrowExceptionWhenSampleRateIsNegative() {
167+
assertThatIllegalArgumentException().isThrownBy(() -> new Criteria().sampleRate(-1));
168+
}
169+
170+
@Test // GH-3726
171+
public void shouldThrowExceptionWhenSampleRateIsGreatedThanOne() {
172+
assertThatIllegalArgumentException().isThrownBy(() -> new Criteria().sampleRate(1.01));
173+
}
174+
159175
@Test // DATAMONGO-1068
160176
public void getCriteriaObjectShouldReturnEmptyDocumentWhenNoCriteriaSpecified() {
161177

Diff for: src/main/asciidoc/reference/mongo-repositories.adoc

+4
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,10 @@ lower / upper bounds (`$gt` / `$gte` & `$lt` / `$lte`) according to `Range`
281281
| `Exists`
282282
| `findByLocationExists(boolean exists)`
283283
| `{"location" : {"$exists" : exists }}`
284+
285+
| `SampleRate`
286+
| `sampleRate(double sampleRate)`
287+
| `{"$sampleRate" : sampleRate }`
284288
|===
285289

286290
NOTE: If the property criterion compares a document, the order of the fields and exact equality in the document matters.

0 commit comments

Comments
 (0)