Call the com.mongodb.DBCollection#distinct method, corresponding to the command line db.collection.distinct("someField") in the question. This will return all the data, so if you just want to get the count, there is no need to use distinct. You can use aggregate directly.
This is the code I actually used. To put it simply, I used group twice to achieve the purpose of deduplicating a certain field in each group
Reference: http://www.cnblogs.com/lori/p/4597341.html
Call the
com.mongodb.DBCollection#distinct
method, corresponding to the command linedb.collection.distinct("someField")
in the question.This will return all the data, so if you just want to get the count, there is no need to use distinct.
You can use aggregate directly.