Ankur
Ankur

Reputation: 51110

MongoDB java driver: get all unique value of a field restricted by another field

Using the MongoDB java driver I would like to get all unique values of a field called "category" where the field called "companyKey" has the value "XYZ".

First question, is this possible?

Second question, do you have any idea what the syntax would look like? I can't find any relevant docs ... pointers to docs would be equally helpful.

Upvotes: 0

Views: 739

Answers (1)

4J41
4J41

Reputation: 5095

Yes.

DBCollection collection=...;
collection.distinct(key, query);

API docs.

Example:

DBObject query = new BasicDBObject("companyKey", "XYZ");
collection.distinct("category", query);

Upvotes: 2

Related Questions