Skip to content

Commit f662d7c

Browse files
committed
Polishing.
Tweak Javadoc. Add since tag, reformat code. Simplify tests. Move documentation bits into the right place. See #3726. Original pull request: #3765.
1 parent 62eb719 commit f662d7c

File tree

4 files changed

+21
-19
lines changed

4 files changed

+21
-19
lines changed

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -394,11 +394,15 @@ public Criteria exists(boolean value) {
394394
/**
395395
* Creates a criterion using the {@literal $sampleRate} operator.
396396
*
397-
* @param sampleRate sample rate to determine number of documents to be randomly selected from the input.
397+
* @param sampleRate sample rate to determine number of documents to be randomly selected from the input. Must be
398+
* between {@code 0} and {@code 1}.
398399
* @return this.
399-
* @see <a href="https://docs.mongodb.com/manual/reference/operator/aggregation/sampleRate/">MongoDB Query operator: $sampleRate</a>
400+
* @see <a href="https://docs.mongodb.com/manual/reference/operator/aggregation/sampleRate/">MongoDB Query operator:
401+
* $sampleRate</a>
402+
* @since 3.3
400403
*/
401404
public Criteria sampleRate(double sampleRate) {
405+
402406
Assert.isTrue(sampleRate >= 0, "The sample rate must be greater than zero!");
403407
Assert.isTrue(sampleRate <= 1, "The sample rate must not be greater than one!");
404408

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

+14-13
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,20 @@
4040
* @author Ziemowit Stolarczyk
4141
* @author Clément Petit
4242
* @author Mark Paluch
43+
* @author James McNee
4344
*/
4445
public class CriteriaUnitTests {
4546

4647
@Test
4748
public void testSimpleCriteria() {
4849
Criteria c = new Criteria("name").is("Bubba");
49-
assertThat(c.getCriteriaObject()).isEqualTo(Document.parse("{ \"name\" : \"Bubba\"}"));
50+
assertThat(c.getCriteriaObject()).isEqualTo("{ \"name\" : \"Bubba\"}");
5051
}
5152

5253
@Test
5354
public void testNotEqualCriteria() {
5455
Criteria c = new Criteria("name").ne("Bubba");
55-
assertThat(c.getCriteriaObject()).isEqualTo(Document.parse("{ \"name\" : { \"$ne\" : \"Bubba\"}}"));
56+
assertThat(c.getCriteriaObject()).isEqualTo("{ \"name\" : { \"$ne\" : \"Bubba\"}}");
5657
}
5758

5859
@Test
@@ -67,7 +68,7 @@ public void buildsIsNullCriteriaCorrectly() {
6768
@Test
6869
public void testChainedCriteria() {
6970
Criteria c = new Criteria("name").is("Bubba").and("age").lt(21);
70-
assertThat(c.getCriteriaObject()).isEqualTo(Document.parse("{ \"name\" : \"Bubba\" , \"age\" : { \"$lt\" : 21}}"));
71+
assertThat(c.getCriteriaObject()).isEqualTo("{ \"name\" : \"Bubba\" , \"age\" : { \"$lt\" : 21}}");
7172
}
7273

7374
@Test(expected = InvalidMongoDbApiUsageException.class)
@@ -153,13 +154,13 @@ public void shouldNegateFollowingSimpleExpression() {
153154
Document co = c.getCriteriaObject();
154155

155156
assertThat(co).isNotNull();
156-
assertThat(co).isEqualTo(Document.parse("{ \"age\" : { \"$not\" : { \"$gt\" : 18}} , \"status\" : \"student\"}"));
157+
assertThat(co).isEqualTo("{ \"age\" : { \"$not\" : { \"$gt\" : 18}} , \"status\" : \"student\"}");
157158
}
158159

159160
@Test // GH-3726
160161
public void shouldBuildCorrectSampleRateOperation() {
161162
Criteria c = new Criteria().sampleRate(0.4);
162-
assertThat(c.getCriteriaObject()).isEqualTo(Document.parse("{ \"$sampleRate\" : 0.4 }"));
163+
assertThat(c.getCriteriaObject()).isEqualTo("{ \"$sampleRate\" : 0.4 }");
163164
}
164165

165166
@Test // GH-3726
@@ -302,7 +303,7 @@ public void shouldAppendBitsAllClearWithIntBitmaskCorrectly() {
302303
Criteria numericBitmaskCriteria = new Criteria("field").bits().allClear(0b101);
303304

304305
assertThat(numericBitmaskCriteria.getCriteriaObject())
305-
.isEqualTo(Document.parse("{ \"field\" : { \"$bitsAllClear\" : 5} }"));
306+
.isEqualTo("{ \"field\" : { \"$bitsAllClear\" : 5} }");
306307
}
307308

308309
@Test // DATAMONGO-1808
@@ -311,7 +312,7 @@ public void shouldAppendBitsAllClearWithPositionListCorrectly() {
311312
Criteria bitPositionsBitmaskCriteria = new Criteria("field").bits().allClear(Arrays.asList(0, 2));
312313

313314
assertThat(bitPositionsBitmaskCriteria.getCriteriaObject())
314-
.isEqualTo(Document.parse("{ \"field\" : { \"$bitsAllClear\" : [ 0, 2 ]} }"));
315+
.isEqualTo("{ \"field\" : { \"$bitsAllClear\" : [ 0, 2 ]} }");
315316
}
316317

317318
@Test // DATAMONGO-1808
@@ -320,7 +321,7 @@ public void shouldAppendBitsAllSetWithIntBitmaskCorrectly() {
320321
Criteria numericBitmaskCriteria = new Criteria("field").bits().allSet(0b101);
321322

322323
assertThat(numericBitmaskCriteria.getCriteriaObject())
323-
.isEqualTo(Document.parse("{ \"field\" : { \"$bitsAllSet\" : 5} }"));
324+
.isEqualTo("{ \"field\" : { \"$bitsAllSet\" : 5} }");
324325
}
325326

326327
@Test // DATAMONGO-1808
@@ -329,7 +330,7 @@ public void shouldAppendBitsAllSetWithPositionListCorrectly() {
329330
Criteria bitPositionsBitmaskCriteria = new Criteria("field").bits().allSet(Arrays.asList(0, 2));
330331

331332
assertThat(bitPositionsBitmaskCriteria.getCriteriaObject())
332-
.isEqualTo(Document.parse("{ \"field\" : { \"$bitsAllSet\" : [ 0, 2 ]} }"));
333+
.isEqualTo("{ \"field\" : { \"$bitsAllSet\" : [ 0, 2 ]} }");
333334
}
334335

335336
@Test // DATAMONGO-1808
@@ -338,7 +339,7 @@ public void shouldAppendBitsAnyClearWithIntBitmaskCorrectly() {
338339
Criteria numericBitmaskCriteria = new Criteria("field").bits().anyClear(0b101);
339340

340341
assertThat(numericBitmaskCriteria.getCriteriaObject())
341-
.isEqualTo(Document.parse("{ \"field\" : { \"$bitsAnyClear\" : 5} }"));
342+
.isEqualTo("{ \"field\" : { \"$bitsAnyClear\" : 5} }");
342343
}
343344

344345
@Test // DATAMONGO-1808
@@ -347,7 +348,7 @@ public void shouldAppendBitsAnyClearWithPositionListCorrectly() {
347348
Criteria bitPositionsBitmaskCriteria = new Criteria("field").bits().anyClear(Arrays.asList(0, 2));
348349

349350
assertThat(bitPositionsBitmaskCriteria.getCriteriaObject())
350-
.isEqualTo(Document.parse("{ \"field\" : { \"$bitsAnyClear\" : [ 0, 2 ]} }"));
351+
.isEqualTo("{ \"field\" : { \"$bitsAnyClear\" : [ 0, 2 ]} }");
351352
}
352353

353354
@Test // DATAMONGO-1808
@@ -356,7 +357,7 @@ public void shouldAppendBitsAnySetWithIntBitmaskCorrectly() {
356357
Criteria numericBitmaskCriteria = new Criteria("field").bits().anySet(0b101);
357358

358359
assertThat(numericBitmaskCriteria.getCriteriaObject())
359-
.isEqualTo(Document.parse("{ \"field\" : { \"$bitsAnySet\" : 5} }"));
360+
.isEqualTo("{ \"field\" : { \"$bitsAnySet\" : 5} }");
360361
}
361362

362363
@Test // DATAMONGO-1808
@@ -365,7 +366,7 @@ public void shouldAppendBitsAnySetWithPositionListCorrectly() {
365366
Criteria bitPositionsBitmaskCriteria = new Criteria("field").bits().anySet(Arrays.asList(0, 2));
366367

367368
assertThat(bitPositionsBitmaskCriteria.getCriteriaObject())
368-
.isEqualTo(Document.parse("{ \"field\" : { \"$bitsAnySet\" : [ 0, 2 ]} }"));
369+
.isEqualTo("{ \"field\" : { \"$bitsAnySet\" : [ 0, 2 ]} }");
369370
}
370371

371372
@Test // DATAMONGO-2002

src/main/asciidoc/reference/mongo-repositories.adoc

-4
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,6 @@ 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 }`
288284
|===
289285

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

src/main/asciidoc/reference/mongodb.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,7 @@ The `Criteria` class provides the following methods, all of which correspond to
12191219
* `Criteria` *orOperator* `(Criteria... criteria)` Creates an or query using the `$or` operator for all of the provided criteria
12201220
* `Criteria` *orOperator* `(Collection<Criteria> criteria)` Creates an or query using the `$or` operator for all of the provided criteria
12211221
* `Criteria` *regex* `(String re)` Creates a criterion using a `$regex`
1222+
* `Criteria` *sampleRate* `(double sampleRate)` Creates a criterion using the `$sampleRate` operator
12221223
* `Criteria` *size* `(int s)` Creates a criterion using the `$size` operator
12231224
* `Criteria` *type* `(int t)` Creates a criterion using the `$type` operator
12241225
* `Criteria` *matchingDocumentStructure* `(MongoJsonSchema schema)` Creates a criterion using the `$jsonSchema` operator for <<mongo.jsonSchema,JSON schema criteria>>. `$jsonSchema` can only be applied on the top level of a query and not property specific. Use the `properties` attribute of the schema to match against nested fields.

0 commit comments

Comments
 (0)