File tree 3 files changed +36
-0
lines changed
main/java/org/springframework/data/mongodb/core/query
test/java/org/springframework/data/mongodb/core/query
src/main/asciidoc/reference
3 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 64
64
* @author Andreas Zink
65
65
* @author Ziemowit Stolarczyk
66
66
* @author Clément Petit
67
+ * @author James McNee
67
68
*/
68
69
public class Criteria implements CriteriaDefinition {
69
70
@@ -390,6 +391,21 @@ public Criteria exists(boolean value) {
390
391
return this ;
391
392
}
392
393
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
+
393
409
/**
394
410
* Creates a criterion using the {@literal $type} operator.
395
411
*
Original file line number Diff line number Diff line change @@ -156,6 +156,22 @@ public void shouldNegateFollowingSimpleExpression() {
156
156
assertThat (co ).isEqualTo (Document .parse ("{ \" age\" : { \" $not\" : { \" $gt\" : 18}} , \" status\" : \" student\" }" ));
157
157
}
158
158
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
+
159
175
@ Test // DATAMONGO-1068
160
176
public void getCriteriaObjectShouldReturnEmptyDocumentWhenNoCriteriaSpecified () {
161
177
Original file line number Diff line number Diff line change @@ -281,6 +281,10 @@ lower / upper bounds (`$gt` / `$gte` & `$lt` / `$lte`) according to `Range`
281
281
| `Exists`
282
282
| `findByLocationExists(boolean exists)`
283
283
| `{"location" : {"$exists" : exists }}`
284
+
285
+ | `SampleRate`
286
+ | `sampleRate(double sampleRate)`
287
+ | `{"$sampleRate" : sampleRate }`
284
288
|===
285
289
286
290
NOTE: If the property criterion compares a document, the order of the fields and exact equality in the document matters.
You can’t perform that action at this time.
0 commit comments