How may I query distinct with the Java MongoDB 3.0 driver?
I am attempting to query unique categories records from a locations
To let you avoid casts for distinct, the MongoCollection API lets you provide the expected type of the distinct values for the field. So if you know they are all strings, for example, you can write:
MongoCursor c =
db.getCollection("locations").distinct("categories", String.class).iterator();
or all numbers:
MongoCursor c =
db.getCollection("locations").distinct("categories", Number.class).iterator();
You can still do:
MongoCursor
if you can't guarantee anything about the types of the values for the field you're querying.