Skip to content

Commit c4c7e4b

Browse files
committed
Add has to document, add tests (#1070)
JAVA-4799
1 parent c5c376a commit c4c7e4b

File tree

7 files changed

+69
-6
lines changed

7 files changed

+69
-6
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@
3333
public interface DocumentExpression extends Expression {
3434

3535
DocumentExpression setField(String fieldName, Expression exp);
36+
/**
37+
* True if {@code this} document has a field with the provided
38+
* {@code fieldName}.
39+
*
40+
* @param fieldName the name of the field.
41+
* @return the resulting value.
42+
*/
43+
BooleanExpression has(String fieldName);
44+
3645

3746
DocumentExpression unsetField(String fieldName);
3847

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ public NumberExpression sum(final Function<? super T, ? extends NumberExpression
597597
@Override
598598
public NumberExpression multiply(final Function<? super T, ? extends NumberExpression> mapper) {
599599
MqlExpression<NumberExpression> array = (MqlExpression<NumberExpression>) this.map(mapper);
600-
return array.reduce(of(0), (NumberExpression a, NumberExpression b) -> a.multiply(b));
600+
return array.reduce(of(1), (NumberExpression a, NumberExpression b) -> a.multiply(b));
601601
}
602602

603603
@Override
@@ -909,6 +909,12 @@ public BooleanExpression has(final StringExpression key) {
909909
return get(key).ne(ofRem());
910910
}
911911

912+
913+
@Override
914+
public BooleanExpression has(final String fieldName) {
915+
return this.has(of(fieldName));
916+
}
917+
912918
static <R extends Expression> R ofRem() {
913919
// $$REMOVE is intentionally not exposed to users
914920
return new MqlExpression<>((cr) -> new MqlExpression.AstPlaceholder(new BsonString("$$REMOVE")))

driver-core/src/test/functional/com/mongodb/client/model/expressions/ArrayExpressionsFunctionalTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,19 @@ public void reduceSumTest() {
200200
ofIntegerArray().sum(a -> a));
201201
}
202202

203+
@Test
204+
public void reduceMultiplyTest() {
205+
assertExpression(
206+
6,
207+
ofIntegerArray(1, 2, 3).multiply(a -> a),
208+
"{'$reduce': {'input': {'$map': {'input': [1, 2, 3], 'in': '$$this'}}, "
209+
+ "'initialValue': 1, 'in': {'$multiply': ['$$value', '$$this']}}}");
210+
// empty array:
211+
assertExpression(
212+
1,
213+
ofIntegerArray().multiply(a -> a));
214+
}
215+
203216
@Test
204217
public void reduceMaxTest() {
205218
assertExpression(

driver-core/src/test/functional/com/mongodb/client/model/expressions/DocumentExpressionsFunctionalTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,17 @@ public void asMapTest() {
261261
DocumentExpression d = ofDoc("{a: 1}");
262262
assertSame(d, d.asMap());
263263
}
264+
265+
266+
@Test
267+
public void hasTest() {
268+
DocumentExpression d = ofDoc("{a: 1}");
269+
assertExpression(
270+
true,
271+
d.has("a"),
272+
"{'$ne': [{'$getField': {'input': {'$literal': {'a': 1}}, 'field': 'a'}}, '$$REMOVE']}");
273+
assertExpression(
274+
false,
275+
d.has("not_a"));
276+
}
264277
}

driver-core/src/test/functional/com/mongodb/client/model/expressions/MapExpressionsFunctionalTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ public void getSetMapTest() {
7777
}
7878

7979
@Test
80-
public void hasMapTest() {
80+
public void hasTest() {
81+
MapExpression<Expression> e = ofMap(BsonDocument.parse("{key: 1}"));
8182
assertExpression(
8283
true,
83-
mapKey123.has(of("key")),
84-
"{'$ne': [{'$getField': {'input': {'$setField': {'field': 'key', 'input': "
85-
+ "{'$literal': {}}, 'value': 123}}, 'field': 'key'}}, '$$REMOVE']}");
84+
e.has(of("key")),
85+
"{'$ne': [{'$getField': {'input': {'$literal': {'key': 1}}, 'field': 'key'}}, '$$REMOVE']}");
8686
assertExpression(
8787
false,
88-
mapKey123.has("not_key"));
88+
e.has("not_key"));
8989
}
9090

9191
@Test

driver-core/src/test/functional/com/mongodb/client/model/expressions/StringExpressionsFunctionalTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ public void substrTest() {
130130
"abc".substring(1, 1 + 1),
131131
of("abc").substr(of(1), of(1)),
132132
"{'$substrCP': ['abc', 1, 1]}");
133+
assertExpression(
134+
"bc",
135+
of("abc").substr(of(1), of(100)),
136+
"{'$substrCP': ['abc', 1, 100]}");
133137

134138
// unicode
135139
assertExpression(

driver-core/src/test/functional/com/mongodb/client/model/expressions/TypeExpressionsFunctionalTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,24 @@ public void parseIntegerTest() {
263263
assertExpression(
264264
longVal,
265265
of(longVal + "").parseInteger());
266+
267+
// failures
268+
assertThrows(MongoCommandException.class, () ->
269+
assertExpression(
270+
"",
271+
of(BsonDocument.parse("{a:'1.5'}")).getString("a").parseInteger()));
272+
assertThrows(MongoCommandException.class, () ->
273+
assertExpression(
274+
"",
275+
of(BsonDocument.parse("{a:'not an integer'}")).getString("a").parseInteger()));
276+
assertThrows(MongoCommandException.class, () ->
277+
assertExpression(
278+
"",
279+
of("1.5").parseInteger()));
280+
assertThrows(MongoCommandException.class, () ->
281+
assertExpression(
282+
"",
283+
of("not an integer").parseInteger()));
266284
}
267285

268286
// non-string

0 commit comments

Comments
 (0)