如何在 mongodb java 驱动程序中获取不同字段的计数

How to get count of distinct field in mongodb java driver

我知道我可以通过这样的查询得到一个不同的列表:

db.collections.distinct("fieldName")

并获取列表的长度

db.collections.distinct("fieldName").length

但是有没有办法在 mongodb-java-驱动程序中获取 length 而不是在聚合查询中?

或者将实现如:

 collection.distinct("fieldName", condition).size() 

效率太低?

对您的代码稍作修改:

db.collections.distinct("fieldName").length