How to get distinct values out of a collection with where condition in mongodb?

Pramod picture Pramod · Apr 25, 2013 · Viewed 9.7k times · Source

How to get distinct values out of a collection with where condition?

For Example, I have Worker class, which contains status and workerId along with other fields, Here i want to retrieve distinct workerId's which has status="ACTIVE";

I have made enough googling but could not find any solution

Any help would be appreciated

Thanks in advance!

Edit

Sorry guys i didn't put my question properly. I want this to be done using morphia not by using mongodb native query.

Can this be done using MapReduce? Help me out please!

Answer

Pramod picture Pramod · May 10, 2013

Sorry for the delay in posting,

BasicDBObject dbObject=new BasicDBObject();
dbObject.append("status","ACTIVE");

this dbObject will act as a where condition,

DBCollection dBCollection = datastore.getCollection(MyEntity.class);
List id=dBCollection.distinct("id",dbObject);

This ID list will contains all distinct ID's whose status="ACTIVE"