Skip to content

Commit 076d334

Browse files
christophstroblmp911de
authored andcommitted
Update count vs. estimatedCount documentation.
Closes #3055 Original pull request: #3541.
1 parent e644692 commit 076d334

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableFindOperation.java

+5
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ default Optional<T> first() {
125125

126126
/**
127127
* Get the number of matching elements.
128+
* <p />
129+
* This method uses an {@link com.mongodb.client.MongoCollection#countDocuments(org.bson.conversions.Bson, com.mongodb.client.model.CountOptions) aggregation
130+
* execution} even for empty {@link Query queries} which may have an impact on performance, but guarantees shard,
131+
* session and transaction compliance. In case an inaccurate count satisfies the applications needs use
132+
* {@link MongoOperations#estimatedCount(String)} for empty queries instead.
128133
*
129134
* @return total number of matching elements.
130135
*/

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java

+24
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,12 @@ <S, T> T findAndReplace(Query query, S replacement, FindAndReplaceOptions option
11601160
* influence on the resulting number of documents found as those values are passed on to the server and potentially
11611161
* limit the range and order within which the server performs the count operation. Use an {@literal unpaged} query to
11621162
* count all matches.
1163+
* <p />
1164+
* This method uses an
1165+
* {@link com.mongodb.client.MongoCollection#countDocuments(org.bson.conversions.Bson, com.mongodb.client.model.CountOptions)
1166+
* aggregation execution} even for empty {@link Query queries} which may have an impact on performance, but guarantees
1167+
* shard, session and transaction compliance. In case an inaccurate count satisfies the applications needs use
1168+
* {@link #estimatedCount(Class)} for empty queries instead.
11631169
*
11641170
* @param query the {@link Query} class that specifies the criteria used to find documents. Must not be
11651171
* {@literal null}.
@@ -1176,6 +1182,12 @@ <S, T> T findAndReplace(Query query, S replacement, FindAndReplaceOptions option
11761182
* influence on the resulting number of documents found as those values are passed on to the server and potentially
11771183
* limit the range and order within which the server performs the count operation. Use an {@literal unpaged} query to
11781184
* count all matches.
1185+
* <p />
1186+
* This method uses an
1187+
* {@link com.mongodb.client.MongoCollection#countDocuments(org.bson.conversions.Bson, com.mongodb.client.model.CountOptions)
1188+
* aggregation execution} even for empty {@link Query queries} which may have an impact on performance, but guarantees
1189+
* shard, session and transaction compliance. In case an inaccurate count satisfies the applications needs use
1190+
* {@link #estimatedCount(String)} for empty queries instead.
11791191
*
11801192
* @param query the {@link Query} class that specifies the criteria used to find documents.
11811193
* @param collectionName must not be {@literal null} or empty.
@@ -1187,6 +1199,9 @@ <S, T> T findAndReplace(Query query, S replacement, FindAndReplaceOptions option
11871199
/**
11881200
* Estimate the number of documents, in the collection {@link #getCollectionName(Class) identified by the given type},
11891201
* based on collection statistics.
1202+
* <p />
1203+
* Please make sure to read the MongoDB reference documentation about limitations on eg. sharded cluster or inside
1204+
* transactions.
11901205
*
11911206
* @param entityClass must not be {@literal null}.
11921207
* @return the estimated number of documents.
@@ -1200,6 +1215,9 @@ default long estimatedCount(Class<?> entityClass) {
12001215

12011216
/**
12021217
* Estimate the number of documents in the given collection based on collection statistics.
1218+
* <p />
1219+
* Please make sure to read the MongoDB reference documentation about limitations on eg. sharded cluster or inside
1220+
* transactions.
12031221
*
12041222
* @param collectionName must not be {@literal null}.
12051223
* @return the estimated number of documents.
@@ -1214,6 +1232,12 @@ default long estimatedCount(Class<?> entityClass) {
12141232
* influence on the resulting number of documents found as those values are passed on to the server and potentially
12151233
* limit the range and order within which the server performs the count operation. Use an {@literal unpaged} query to
12161234
* count all matches.
1235+
* <p />
1236+
* This method uses an
1237+
* {@link com.mongodb.client.MongoCollection#countDocuments(org.bson.conversions.Bson, com.mongodb.client.model.CountOptions)
1238+
* aggregation execution} even for empty {@link Query queries} which may have an impact on performance, but guarantees
1239+
* shard, session and transaction compliance. In case an inaccurate count satisfies the applications needs use
1240+
* {@link #estimatedCount(String)} for empty queries instead.
12171241
*
12181242
* @param query the {@link Query} class that specifies the criteria used to find documents. Must not be
12191243
* {@literal null}.

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveFindOperation.java

+6
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ interface TerminatingFind<T> {
106106

107107
/**
108108
* Get the number of matching elements.
109+
* <p />
110+
* This method uses an
111+
* {@link com.mongodb.reactivestreams.client.MongoCollection#countDocuments(org.bson.conversions.Bson, com.mongodb.client.model.CountOptions)
112+
* aggregation execution} even for empty {@link Query queries} which may have an impact on performance, but
113+
* guarantees shard, session and transaction compliance. In case an inaccurate count satisfies the applications
114+
* needs use {@link ReactiveMongoOperations#estimatedCount(String)} for empty queries instead.
109115
*
110116
* @return {@link Mono} emitting total number of matching elements. Never {@literal null}.
111117
*/

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoOperations.java

+24
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,12 @@ <S, T> Mono<T> findAndReplace(Query query, S replacement, FindAndReplaceOptions
940940
* influence on the resulting number of documents found as those values are passed on to the server and potentially
941941
* limit the range and order within which the server performs the count operation. Use an {@literal unpaged} query to
942942
* count all matches.
943+
* <p />
944+
* This method uses an
945+
* {@link com.mongodb.reactivestreams.client.MongoCollection#countDocuments(org.bson.conversions.Bson, com.mongodb.client.model.CountOptions)
946+
* aggregation execution} even for empty {@link Query queries} which may have an impact on performance, but guarantees
947+
* shard, session and transaction compliance. In case an inaccurate count satisfies the applications needs use
948+
* {@link #estimatedCount(Class)} for empty queries instead.
943949
*
944950
* @param query the {@link Query} class that specifies the criteria used to find documents. Must not be
945951
* {@literal null}.
@@ -956,6 +962,12 @@ <S, T> Mono<T> findAndReplace(Query query, S replacement, FindAndReplaceOptions
956962
* influence on the resulting number of documents found as those values are passed on to the server and potentially
957963
* limit the range and order within which the server performs the count operation. Use an {@literal unpaged} query to
958964
* count all matches.
965+
* <p />
966+
* This method uses an
967+
* {@link com.mongodb.reactivestreams.client.MongoCollection#countDocuments(org.bson.conversions.Bson, com.mongodb.client.model.CountOptions)
968+
* aggregation execution} even for empty {@link Query queries} which may have an impact on performance, but guarantees
969+
* shard, session and transaction compliance. In case an inaccurate count satisfies the applications needs use
970+
* {@link #estimatedCount(String)} for empty queries instead.
959971
*
960972
* @param query the {@link Query} class that specifies the criteria used to find documents.
961973
* @param collectionName must not be {@literal null} or empty.
@@ -971,6 +983,12 @@ <S, T> Mono<T> findAndReplace(Query query, S replacement, FindAndReplaceOptions
971983
* influence on the resulting number of documents found as those values are passed on to the server and potentially
972984
* limit the range and order within which the server performs the count operation. Use an {@literal unpaged} query to
973985
* count all matches.
986+
* <p />
987+
* This method uses an
988+
* {@link com.mongodb.reactivestreams.client.MongoCollection#countDocuments(org.bson.conversions.Bson, com.mongodb.client.model.CountOptions)
989+
* aggregation execution} even for empty {@link Query queries} which may have an impact on performance, but guarantees
990+
* shard, session and transaction compliance. In case an inaccurate count satisfies the applications needs use
991+
* {@link #estimatedCount(String)} for empty queries instead.
974992
*
975993
* @param query the {@link Query} class that specifies the criteria used to find documents. Must not be
976994
* {@literal null}.
@@ -983,6 +1001,9 @@ <S, T> Mono<T> findAndReplace(Query query, S replacement, FindAndReplaceOptions
9831001
/**
9841002
* Estimate the number of documents, in the collection {@link #getCollectionName(Class) identified by the given type},
9851003
* based on collection statistics.
1004+
* <p />
1005+
* Please make sure to read the MongoDB reference documentation about limitations on eg. sharded cluster or inside
1006+
* transactions.
9861007
*
9871008
* @param entityClass must not be {@literal null}.
9881009
* @return a {@link Mono} emitting the estimated number of documents.
@@ -996,6 +1017,9 @@ default Mono<Long> estimatedCount(Class<?> entityClass) {
9961017

9971018
/**
9981019
* Estimate the number of documents in the given collection based on collection statistics.
1020+
* <p />
1021+
* Please make sure to read the MongoDB reference documentation about limitations on eg. sharded cluster or inside
1022+
* transactions.
9991023
*
10001024
* @param collectionName must not be {@literal null}.
10011025
* @return a {@link Mono} emitting the estimated number of documents.

src/main/asciidoc/reference/mongodb.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -2221,6 +2221,7 @@ With the introduction of <<mongo.transactions>> this was no longer possible beca
22212221
So in version 2.x `MongoOperations.count()` would use the collection statistics if no transaction was in progress, and the aggregation variant if so.
22222222

22232223
As of Spring Data MongoDB 3.x any `count` operation uses regardless the existence of filter criteria the aggregation-based count approach via MongoDBs `countDocuments`.
2224+
If the application is fine with the limitations of working upon collection statistics `MongoOperations.estimatedCount()` offers an alternative.
22242225

22252226
[NOTE]
22262227
====

0 commit comments

Comments
 (0)