Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix behaviour of using $ne on a field of an object in an array. #221

Merged
merged 1 commit into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ private boolean checkMatch(Object queryValue, List<String> keys, Object value) {
Object allQuery = query.get(QueryOperator.ALL.getValue());
return checkMatchesAllDocuments(allQuery, keys, value);
}
if (query.containsKey(QueryOperator.NOT_EQUALS.getValue())) {
Object notEqualQuery = query.get(QueryOperator.NOT_EQUALS.getValue());
return !checkMatchesAnyDocument(notEqualQuery, keys, value);
}
if (query.containsKey(QueryOperator.NOT_IN.getValue())) {
Object notInQueryValue = query.get(QueryOperator.NOT_IN.getValue());
Document inQuery = new Document(QueryOperator.IN.getValue(), notInQueryValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,18 @@ void testMatchesNotEqual() throws Exception {
assertThat(matcher.matches(document, query)).isTrue();
}

// https://github.com/bwaldvogel/mongo-java-server/issues/220
@Test
void testMatchesNeFieldInArray() throws Exception {
Document query = json("'tags.value': {$ne: 'B'}");

Document document = json("_id: 1, tags: [{'value': 'A'}, {'value': 'D'}]");
assertThat(matcher.matches(document, query)).isTrue();

document = json("_id: 1, tags: [{'value': 'B'}, {'value': 'A'}]");
assertThat(matcher.matches(document, query)).isFalse();
}

// http://docs.mongodb.org/v3.0/reference/operator/query/eq/#op._S_eq
@Test
void testMatchesEqual() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2307,6 +2307,19 @@ void testMatchesNinFieldInArray() throws Exception {
.containsExactly(json("_id: 1, tags: [{'value': 'A'}, {'value': 'D'}]"));
}

// https://github.com/bwaldvogel/mongo-java-server/issues/220
@Test
void testMatchesNeFieldInArray() throws Exception {
collection.insertOne(json("_id: 1, tags: [{'value': 'A'}, {'value': 'D'}]"));
collection.insertOne(json("_id: 2, tags: [{'value': 'A'}, {'value': 'B'}]"));
collection.insertOne(json("_id: 3, tags: [{'value': 'A'}, {'value': 'C'}]"));

assertThat(collection.find(json("'tags.value': {$ne: 'B'}")))
.containsExactly(
json("_id: 1, tags: [{'value': 'A'}, {'value': 'D'}]"),
json("_id: 3, tags: [{'value': 'A'}, {'value': 'C'}]"));
}

// https://github.com/bwaldvogel/mongo-java-server/issues/7
@Test
void testMatchesNotIn() throws Exception {
Expand Down