Reputation: 807
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!
Upvotes: 4
Views: 10047
Reputation: 807
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"
Upvotes: 14
Reputation: 80176
distinct is supported as a command instead of a query (not sure why). Try the below
db.runCommand (
{
distinct: 'name-of-collection',
key: 'worker-id',
query: { 'status': 'ACTIVE' }
}
)
Upvotes: 0