I want to get a list of unique values from a List field called categories.
I am using Java and MongoDB. Most of the examples and docs I can see seem to suggest I need to do something like what I have below:
public static List<String> listCategories(String input) {
Datastore ds = Dao.instance().getDatabase();
BasicDBObject dbObject=new BasicDBObject("categories", input);
DBCollection dBCollection = ds.getCollection(Product.class);
List<String> categories = dBCollection.distinct("categories",dbObject);
return categories;
}
However when I test it using this code:
@Test
public void testListCategories(){
List<String> categories = Product.listCategories("S");
Assert.assertTrue(categories.size() > 0);
}
The test fails even though I know there are categories that start with S (have also tried a few others just to be sure).
Is it even possible to do this, if so do you have any pointers?
Thanks