Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ public DataResult<Long> count(ClientSession clientSession, Bson query) {
return endQuery(Collections.emptyList(), l, start);
}

public DataResult<Long> estimatedCount() {
long start = startQuery();
long l = mongoDBNativeQuery.estimatedCount();
return endQuery(Collections.emptyList(), l, start);
}

public DataResult<?> distinct(String key, Bson query) {
long start = startQuery();
List l = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public long count(ClientSession clientSession, Bson query) {
}
}

public long estimatedCount() {
return dbCollection.estimatedDocumentCount();
}

public DistinctIterable<Document> distinct(String key) {
return distinct(key, null, Document.class);
}
Expand Down Expand Up @@ -235,7 +239,11 @@ public <T> MongoDBIterator<T> find(ClientSession clientSession, Bson query, Bson
Future<Long> countFuture = null;
if (options != null && options.getBoolean(QueryOptions.COUNT)) {
ExecutorService executor = Executors.newSingleThreadExecutor();
countFuture = executor.submit(() -> count(clientSession, query));
if (clientSession == null && (query == null || query.equals(Filters.empty()) || query.toBsonDocument().isEmpty())) {
countFuture = executor.submit(this::estimatedCount);
} else {
countFuture = executor.submit(() -> count(clientSession, query));
}
}

FindIterable<Document> findIterable = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@ public void testCount1() throws Exception {
assertEquals("The number must be equals", N, queryResult.getNumMatches());
}

@Test
public void testEstimatedCount() {
DataResult<Long> queryResult = mongoDBCollection.estimatedCount();
assertEquals("The number of documents must be equals", N, queryResult.getNumMatches());
}

@Test
public void testDistinct1() throws Exception {
DataResult<Integer> queryResult = mongoDBCollection.distinct("age", null, Integer.class);
Expand Down
Loading