Skip to content

Commit

Permalink
Fix distinct queries on fields with array values #70
Browse files Browse the repository at this point in the history
  • Loading branch information
bwaldvogel committed Jun 10, 2019
1 parent 2424ba2 commit 7449324
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,11 @@ public synchronized Document handleDistinct(Document query) {
for (Document document : queryDocuments(filter, null, 0, 0)) {
Object value = Utils.getSubdocumentValue(document, key);
if (!(value instanceof Missing)) {
values.add(value);
if (value instanceof Collection) {
values.addAll((Collection<?>) value);
} else {
values.add(value);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,18 @@ public void testDistinctUuids() throws Exception {
);
}

// https://github.com/bwaldvogel/mongo-java-server/issues/70
@Test
public void testDistinctArrayField() throws Exception {
collection.insertOne(json("_id: 1, n: null"));
collection.insertOne(json("_id: 2").append("n", Arrays.asList(1, 2, 3)));
collection.insertOne(json("_id: 3").append("n", Arrays.asList(3, 4, 5)));
collection.insertOne(json("_id: 4").append("n", 6));

assertThat(toArray(collection.distinct("n", Integer.class)))
.containsExactly(null, 1, 2, 3, 4, 5, 6);
}

@Test
public void testInsertQueryAndSortBinaryTypes() throws Exception {
byte[] highBytes = new byte[16];
Expand Down

0 comments on commit 7449324

Please sign in to comment.