Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 772
Pramod Avatar asked Dec 09 '22 15:12

Pramod


1 Answers

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"

like image 57
Pramod Avatar answered Apr 29 '23 19:04

Pramod