Skip to content

Commit 54454ae

Browse files
committed
JAVA-2617: Add Filters methods for $expr and $jsonSchema
1 parent a7e0383 commit 54454ae

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

driver-core/src/main/com/mongodb/client/model/Filters.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,20 @@ public static Bson where(final String javaScriptExpression) {
468468
return new BsonDocument("$where", new BsonString(javaScriptExpression));
469469
}
470470

471+
/**
472+
* Creates a filter that matches all documents that validate against the given JSON schema document.
473+
*
474+
* @param expression the aggregation expression
475+
* @param <TExpression> the expression type
476+
* @return the filter
477+
* @since 3.6
478+
* @mongodb.server.release 3.6
479+
* @mongodb.driver.manual reference/operator/query/expr/ $expr
480+
*/
481+
public static <TExpression> Bson expr(final TExpression expression) {
482+
return new SimpleEncodingFilter<TExpression>("$expr", expression);
483+
}
484+
471485
/**
472486
* Creates a filter that matches all documents where the value of a field is an array that contains all the specified values.
473487
*
@@ -828,6 +842,19 @@ public static Bson nearSphere(final String fieldName, final double x, final doub
828842
return createNearFilterDocument(fieldName, x, y, maxDistance, minDistance, "$nearSphere");
829843
}
830844

845+
/**
846+
* Creates a filter that matches all documents that validate against the given JSON schema document.
847+
*
848+
* @param schema the JSON schema to validate against
849+
* @return the filter
850+
* @since 3.6
851+
* @mongodb.server.release 3.6
852+
* @mongodb.driver.manual reference/operator/query/jsonSchema/ $jsonSchema
853+
*/
854+
public static Bson jsonSchema(final Bson schema) {
855+
return new SimpleEncodingFilter<Bson>("$jsonSchema", schema);
856+
}
857+
831858
private static Bson createNearFilterDocument(final String fieldName, final double x, final double y, final Double maxDistance,
832859
final Double minDistance, final String operator) {
833860
BsonDocument nearFilter = new BsonDocument(operator, new BsonArray(Arrays.asList(new BsonDouble(x), new BsonDouble(y))));

driver-core/src/test/functional/com/mongodb/client/model/FiltersFunctionalSpecification.groovy

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ import static com.mongodb.client.model.Filters.bitsAnySet
3535
import static com.mongodb.client.model.Filters.elemMatch
3636
import static com.mongodb.client.model.Filters.eq
3737
import static com.mongodb.client.model.Filters.exists
38+
import static com.mongodb.client.model.Filters.expr
3839
import static com.mongodb.client.model.Filters.gt
3940
import static com.mongodb.client.model.Filters.gte
41+
import static com.mongodb.client.model.Filters.jsonSchema
4042
import static com.mongodb.client.model.Filters.lt
4143
import static com.mongodb.client.model.Filters.lte
4244
import static com.mongodb.client.model.Filters.mod
@@ -296,4 +298,17 @@ class FiltersFunctionalSpecification extends OperationFunctionalSpecification {
296298
expect:
297299
find(where('Array.isArray(this.a)')) == [a, b]
298300
}
301+
302+
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
303+
def '$expr'() {
304+
expect:
305+
find(expr(Document.parse('{ $eq: [ "$x" , 3 ] } '))) == [c]
306+
}
307+
308+
309+
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
310+
def '$jsonSchema'() {
311+
expect:
312+
find(jsonSchema(Document.parse('{ bsonType : "object", properties: { x : {type : "number", minimum : 2} } } '))) == [b, c]
313+
}
299314
}

driver-core/src/test/unit/com/mongodb/client/model/FiltersSpecification.groovy

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import org.bson.BsonArray
2424
import org.bson.BsonDocument
2525
import org.bson.BsonInt32
2626
import org.bson.BsonInt64
27+
import org.bson.BsonString
2728
import org.bson.BsonType
2829
import org.bson.Document
2930
import org.bson.codecs.BsonValueCodecProvider
@@ -45,6 +46,7 @@ import static com.mongodb.client.model.Filters.bitsAnyClear
4546
import static com.mongodb.client.model.Filters.bitsAnySet
4647
import static com.mongodb.client.model.Filters.elemMatch
4748
import static com.mongodb.client.model.Filters.eq
49+
import static com.mongodb.client.model.Filters.expr
4850
import static com.mongodb.client.model.Filters.geoIntersects
4951
import static com.mongodb.client.model.Filters.geoWithin
5052
import static com.mongodb.client.model.Filters.geoWithinBox
@@ -53,6 +55,7 @@ import static com.mongodb.client.model.Filters.geoWithinCenterSphere
5355
import static com.mongodb.client.model.Filters.geoWithinPolygon
5456
import static com.mongodb.client.model.Filters.gt
5557
import static com.mongodb.client.model.Filters.gte
58+
import static com.mongodb.client.model.Filters.jsonSchema
5659
import static com.mongodb.client.model.Filters.lt
5760
import static com.mongodb.client.model.Filters.lte
5861
import static com.mongodb.client.model.Filters.mod
@@ -275,6 +278,12 @@ class FiltersSpecification extends Specification {
275278
toBson(where('this.credits == this.debits')) == parse('{$where: "this.credits == this.debits"}')
276279
}
277280

281+
def 'should render $expr'() {
282+
expect:
283+
toBson(expr(new BsonDocument('$gt', new BsonArray([new BsonString('$spent'), new BsonString('$budget')])))) ==
284+
parse('{$expr: { $gt: [ "$spent" , "$budget" ] } }')
285+
}
286+
278287
def 'should render $geoWithin'() {
279288
given:
280289
def polygon = new Polygon([new Position([40.0d, 18.0d]),
@@ -615,6 +624,15 @@ class FiltersSpecification extends Specification {
615624
}''')
616625
}
617626

627+
def 'should render $jsonSchema'() {
628+
expect:
629+
toBson(jsonSchema(new BsonDocument('bsonType', new BsonString('object')))) == parse( '''{
630+
$jsonSchema : {
631+
bsonType : "object"
632+
}
633+
}''')
634+
}
635+
618636
def 'should render with iterable value'() {
619637
expect:
620638
toBson(eq('x', new Document())) == parse('''{

0 commit comments

Comments
 (0)