Skip to content
Closed

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ public class BitsAllClearSelector extends SingleConditionQuerySelector {
@Override
protected QueryOperation parseValue(String fieldName, Object value) {
Objects.requireNonNull(fieldName);
return value instanceof Long ? new BitsAllClearOperation(fieldName, (Long) value) : null;
// MongoDB accepts any integer bitmask, so it can arrive as an Integer as well as a Long
return value instanceof Integer || value instanceof Long
? new BitsAllClearOperation(fieldName, ((Number) value).longValue())
: null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ public class BitsAllSetSelector extends SingleConditionQuerySelector {
@Override
protected QueryOperation parseValue(String fieldName, Object value) {
Objects.requireNonNull(fieldName);
return value instanceof Long ? new BitsAllSetOperation(fieldName, (Long) value) : null;
// MongoDB accepts any integer bitmask, so it can arrive as an Integer as well as a Long
return value instanceof Integer || value instanceof Long
? new BitsAllSetOperation(fieldName, ((Number) value).longValue())
: null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ public class BitsAnyClearSelector extends SingleConditionQuerySelector {
@Override
protected QueryOperation parseValue(String fieldName, Object value) {
Objects.requireNonNull(fieldName);
return value instanceof Long ? new BitsAnyClearOperation(fieldName, (Long) value) : null;
// MongoDB accepts any integer bitmask, so it can arrive as an Integer as well as a Long
return value instanceof Integer || value instanceof Long
? new BitsAnyClearOperation(fieldName, ((Number) value).longValue())
: null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ public class BitsAnySetSelector extends SingleConditionQuerySelector {
@Override
protected QueryOperation parseValue(String fieldName, Object value) {
Objects.requireNonNull(fieldName);
return value instanceof Long ? new BitsAnySetOperation(fieldName, (Long) value) : null;
// MongoDB accepts any integer bitmask, so it can arrive as an Integer as well as a Long
return value instanceof Integer || value instanceof Long
? new BitsAnySetOperation(fieldName, ((Number) value).longValue())
: null;
}

@Override
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -1616,14 +1616,24 @@ void testParseInvalidBitwiseValues() {
assertAll(
() -> assertInvalidQuery(
new Document("flags", new Document("$bitsAllClear", "5"))),
() -> assertInvalidQuery(new Document("flags", new Document("$bitsAllSet", 5))),
() -> assertInvalidQuery(
new Document("flags", new Document("$bitsAnyClear", true))),
() -> assertInvalidQuery(
new Document("flags", new Document("$bitsAnySet", new Document("bit", 1))))
);
}

@Test
void testParseBitwiseValuesAcceptsIntegerAndLongBitmasks() {
// MongoDB accepts any integer bitmask, so it can arrive as an Integer as well as a Long
assertTrue(parser.parse(
new Document("flags", new Document("$bitsAllSet", 5))) instanceof BitsAllSetOperation);
assertTrue(parser.parse(
new Document("flags", new Document("$bitsAllSet", 5L))) instanceof BitsAllSetOperation);
assertTrue(parser.parse(
new Document("flags", new Document("$bitsAnyClear", 5))) instanceof BitsAnyClearOperation);
}

@Test
void testParseNearSphereWithoutOptionalDistances() {
Document geometry = new Document("type", "Point")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,34 @@ public ResponseEntity<Void> findAll() {
return executeQuery(new Document("tags", new Document("$all", Arrays.asList("a", "b"))));
}

/**
* "name" holds a string, so it is of a different, incomparable BSON type than the
* number being compared against. MongoDB considers such values different, so $ne holds.
*/
@GetMapping("neCrossType")
public ResponseEntity<Void> findNeCrossType() {
return executeQuery(new Document("name", new Document("$ne", 42)));
}

/**
* "tags" holds an array, so $nin must look at its elements: only a document whose
* array does not hold "a" satisfies this.
*/
@GetMapping("ninArrayField")
public ResponseEntity<Void> findNinArrayField() {
return executeQuery(new Document("tags", new Document("$nin", Arrays.asList("a"))));
}

/**
* $exists with "false" matches a document in which the field is absent, so negating it
* must match only the documents that do have the field.
*/
@GetMapping("notExistsFalse")
public ResponseEntity<Void> findNotExistsFalse() {
return executeQuery(new Document("description",
new Document("$not", new Document("$exists", false))));
}

@GetMapping("type")
public ResponseEntity<Void> findType() {
return executeQuery(new Document("name", new Document("$type", 2)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public void testRunEM() throws Throwable {
assertHasAtLeastOne(solution, HttpVerb.GET, 200, "/mongoqueries/bitsAllSet", null);
assertHasAtLeastOne(solution, HttpVerb.GET, 200, "/mongoqueries/bitsAnyClear", null);
assertHasAtLeastOne(solution, HttpVerb.GET, 200, "/mongoqueries/all", null);
assertHasAtLeastOne(solution, HttpVerb.GET, 200, "/mongoqueries/neCrossType", null);
assertHasAtLeastOne(solution, HttpVerb.GET, 200, "/mongoqueries/ninArrayField", null);
assertHasAtLeastOne(solution, HttpVerb.GET, 200, "/mongoqueries/notExistsFalse", null);
assertHasAtLeastOne(solution, HttpVerb.GET, 200, "/mongoqueries/type", null);
assertHasAtLeastOne(solution, HttpVerb.GET, 200, "/mongoqueries/exists", null);
assertHasAtLeastOne(solution, HttpVerb.GET, 200, "/mongoqueries/nor", null);
Expand Down