Add support for $sampleRate criteria.
Closes #3726 Original pull request: #3765.
This commit is contained in:
@@ -64,6 +64,7 @@ import com.mongodb.BasicDBList;
|
||||
* @author Andreas Zink
|
||||
* @author Ziemowit Stolarczyk
|
||||
* @author Clément Petit
|
||||
* @author James McNee
|
||||
*/
|
||||
public class Criteria implements CriteriaDefinition {
|
||||
|
||||
@@ -390,6 +391,21 @@ public class Criteria implements CriteriaDefinition {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a criterion using the {@literal $sampleRate} operator.
|
||||
*
|
||||
* @param sampleRate sample rate to determine number of documents to be randomly selected from the input.
|
||||
* @return this.
|
||||
* @see <a href="https://docs.mongodb.com/manual/reference/operator/aggregation/sampleRate/">MongoDB Query operator: $sampleRate</a>
|
||||
*/
|
||||
public Criteria sampleRate(double sampleRate) {
|
||||
Assert.isTrue(sampleRate >= 0, "The sample rate must be greater than zero!");
|
||||
Assert.isTrue(sampleRate <= 1, "The sample rate must not be greater than one!");
|
||||
|
||||
criteria.put("$sampleRate", sampleRate);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a criterion using the {@literal $type} operator.
|
||||
*
|
||||
|
||||
@@ -156,6 +156,22 @@ public class CriteriaUnitTests {
|
||||
assertThat(co).isEqualTo(Document.parse("{ \"age\" : { \"$not\" : { \"$gt\" : 18}} , \"status\" : \"student\"}"));
|
||||
}
|
||||
|
||||
@Test // GH-3726
|
||||
public void shouldBuildCorrectSampleRateOperation() {
|
||||
Criteria c = new Criteria().sampleRate(0.4);
|
||||
assertThat(c.getCriteriaObject()).isEqualTo(Document.parse("{ \"$sampleRate\" : 0.4 }"));
|
||||
}
|
||||
|
||||
@Test // GH-3726
|
||||
public void shouldThrowExceptionWhenSampleRateIsNegative() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new Criteria().sampleRate(-1));
|
||||
}
|
||||
|
||||
@Test // GH-3726
|
||||
public void shouldThrowExceptionWhenSampleRateIsGreatedThanOne() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new Criteria().sampleRate(1.01));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1068
|
||||
public void getCriteriaObjectShouldReturnEmptyDocumentWhenNoCriteriaSpecified() {
|
||||
|
||||
|
||||
@@ -281,6 +281,10 @@ lower / upper bounds (`$gt` / `$gte` & `$lt` / `$lte`) according to `Range`
|
||||
| `Exists`
|
||||
| `findByLocationExists(boolean exists)`
|
||||
| `{"location" : {"$exists" : exists }}`
|
||||
|
||||
| `SampleRate`
|
||||
| `sampleRate(double sampleRate)`
|
||||
| `{"$sampleRate" : sampleRate }`
|
||||
|===
|
||||
|
||||
NOTE: If the property criterion compares a document, the order of the fields and exact equality in the document matters.
|
||||
|
||||
Reference in New Issue
Block a user