Skip to content

Commit 042ce2f

Browse files
authored
Merge pull request #221 from MamboIO/main
Fix behaviour of using $ne on a field of an object in an array.
2 parents f20f3cb + cfab6c0 commit 042ce2f

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

core/src/main/java/de/bwaldvogel/mongo/backend/DefaultQueryMatcher.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ private boolean checkMatch(Object queryValue, List<String> keys, Object value) {
140140
Object allQuery = query.get(QueryOperator.ALL.getValue());
141141
return checkMatchesAllDocuments(allQuery, keys, value);
142142
}
143+
if (query.containsKey(QueryOperator.NOT_EQUALS.getValue())) {
144+
Object notEqualQuery = query.get(QueryOperator.NOT_EQUALS.getValue());
145+
return !checkMatchesAnyDocument(notEqualQuery, keys, value);
146+
}
143147
if (query.containsKey(QueryOperator.NOT_IN.getValue())) {
144148
Object notInQueryValue = query.get(QueryOperator.NOT_IN.getValue());
145149
Document inQuery = new Document(QueryOperator.IN.getValue(), notInQueryValue);

core/src/test/java/de/bwaldvogel/mongo/backend/DefaultQueryMatcherTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,18 @@ void testMatchesNotEqual() throws Exception {
347347
assertThat(matcher.matches(document, query)).isTrue();
348348
}
349349

350+
// https://github.com/bwaldvogel/mongo-java-server/issues/220
351+
@Test
352+
void testMatchesNeFieldInArray() throws Exception {
353+
Document query = json("'tags.value': {$ne: 'B'}");
354+
355+
Document document = json("_id: 1, tags: [{'value': 'A'}, {'value': 'D'}]");
356+
assertThat(matcher.matches(document, query)).isTrue();
357+
358+
document = json("_id: 1, tags: [{'value': 'B'}, {'value': 'A'}]");
359+
assertThat(matcher.matches(document, query)).isFalse();
360+
}
361+
350362
// http://docs.mongodb.org/v3.0/reference/operator/query/eq/#op._S_eq
351363
@Test
352364
void testMatchesEqual() throws Exception {

test-common/src/main/java/de/bwaldvogel/mongo/backend/AbstractBackendTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2307,6 +2307,19 @@ void testMatchesNinFieldInArray() throws Exception {
23072307
.containsExactly(json("_id: 1, tags: [{'value': 'A'}, {'value': 'D'}]"));
23082308
}
23092309

2310+
// https://github.com/bwaldvogel/mongo-java-server/issues/220
2311+
@Test
2312+
void testMatchesNeFieldInArray() throws Exception {
2313+
collection.insertOne(json("_id: 1, tags: [{'value': 'A'}, {'value': 'D'}]"));
2314+
collection.insertOne(json("_id: 2, tags: [{'value': 'A'}, {'value': 'B'}]"));
2315+
collection.insertOne(json("_id: 3, tags: [{'value': 'A'}, {'value': 'C'}]"));
2316+
2317+
assertThat(collection.find(json("'tags.value': {$ne: 'B'}")))
2318+
.containsExactly(
2319+
json("_id: 1, tags: [{'value': 'A'}, {'value': 'D'}]"),
2320+
json("_id: 3, tags: [{'value': 'A'}, {'value': 'C'}]"));
2321+
}
2322+
23102323
// https://github.com/bwaldvogel/mongo-java-server/issues/7
23112324
@Test
23122325
void testMatchesNotIn() throws Exception {

0 commit comments

Comments
 (0)