最新的稳定版请使用 Spring Data MongoDB 4.3.1Spring中文文档

最新的稳定版请使用 Spring Data MongoDB 4.3.1Spring中文文档

模板 API 提供了多种方法来计算符合给定条件的文档数量。 其中之一概述如下。Spring中文文档

template.query(Person.class)
    .matching(query(where("firstname").is("luke")))
    .count();

在 SpringData MongoDB 的 3.x 版本中,计数操作使用 MongoDB 内部集合统计信息。 随着MongoDB事务的引入,这不再可能,因为统计信息无法正确反映需要基于聚合的计数方法的事务期间的潜在变化。 因此,在版本 2.x 中,如果没有事务正在进行,则使用集合统计信息,如果正在进行,则使用聚合变体。MongoOperations.count()Spring中文文档

从Spring Data MongoDB 3.x开始,任何操作都使用,无论是否存在过滤条件,通过MongoDBs的基于聚合的计数方法。 如果应用程序可以解决处理收集统计信息的局限性,则提供另一种选择。countcountDocumentsMongoOperations.estimatedCount()Spring中文文档

通过设置为 MongoTemplate#count(...),使用空筛选器查询的操作将被委托给 ,只要没有事务处于活动状态并且模板未绑定到会话。 仍然可以通过 获得确切的数字,但可能会加快速度。MongoTemplate#useEstimatedCount(…​)trueestimatedCountMongoTemplate#exactCountSpring中文文档

通过设置为 MongoTemplate#count(...),使用空筛选器查询的操作将被委托给 ,只要没有事务处于活动状态并且模板未绑定到会话。 仍然可以通过 获得确切的数字,但可能会加快速度。MongoTemplate#useEstimatedCount(…​)trueestimatedCountMongoTemplate#exactCountSpring中文文档

MongoDB的本机方法和聚合,不支持,但需要与或不支持(见 jira.mongodb.org/browse/SERVER-37043)。countDocuments$match$near$nearSphere$geoWithin$center$centerSphere$minDistanceSpring中文文档

因此,给定的操作将被重写,以便使用 -/ 绕过如下所示的问题。QuerycountReactiveMongoTemplateSpring中文文档

{ location : { $near : [-73.99171, 40.738868], $maxDistance : 1.1 } } (1)
{ location : { $geoWithin : { $center: [ [-73.99171, 40.738868], 1.1] } } } (2)

{ location : { $near : [-73.99171, 40.738868], $minDistance : 0.1, $maxDistance : 1.1 } } (3)
{$and :[ { $nor :[ { location :{ $geoWithin :{ $center :[ [-73.99171, 40.738868 ], 0.01] } } } ]}, { location :{ $geoWithin :{ $center :[ [-73.99171, 40.738868 ], 1.1] } } } ] } (4)
1 使用 对源查询进行计数。$near
2 重写的查询现在使用 .$geoWithin$center
3 使用 with 和 对源查询进行计数。$near$minDistance$maxDistance
4 重写了查询,现在是 critierias 的组合,以解决不受支持的问题。$nor$geowithin$minDistance

MongoDB的本机方法和聚合,不支持,但需要与或不支持(见 jira.mongodb.org/browse/SERVER-37043)。countDocuments$match$near$nearSphere$geoWithin$center$centerSphere$minDistanceSpring中文文档

因此,给定的操作将被重写,以便使用 -/ 绕过如下所示的问题。QuerycountReactiveMongoTemplateSpring中文文档

{ location : { $near : [-73.99171, 40.738868], $maxDistance : 1.1 } } (1)
{ location : { $geoWithin : { $center: [ [-73.99171, 40.738868], 1.1] } } } (2)

{ location : { $near : [-73.99171, 40.738868], $minDistance : 0.1, $maxDistance : 1.1 } } (3)
{$and :[ { $nor :[ { location :{ $geoWithin :{ $center :[ [-73.99171, 40.738868 ], 0.01] } } } ]}, { location :{ $geoWithin :{ $center :[ [-73.99171, 40.738868 ], 1.1] } } } ] } (4)
1 使用 对源查询进行计数。$near
2 重写的查询现在使用 .$geoWithin$center
3 使用 with 和 对源查询进行计数。$near$minDistance$maxDistance
4 重写了查询,现在是 critierias 的组合,以解决不受支持的问题。$nor$geowithin$minDistance
1 使用 对源查询进行计数。$near
2 重写的查询现在使用 .$geoWithin$center
3 使用 with 和 对源查询进行计数。$near$minDistance$maxDistance
4 重写了查询,现在是 critierias 的组合,以解决不受支持的问题。$nor$geowithin$minDistance