Skip to content
Advertisement

Panache with MongoDB find distinct

I have documents with “tags” arrays as properties. Now I want to query all distinct tag-items.

JavaScript

Solution in mongo shell:

JavaScript

which gives me:

JavaScript

But how can I achieve the same result with Panache? The PanacheMongoEntity does not offer a specific distinct method. Nor do i know how to use the find method to achieve my goal or if it even is possible using this method.

All I could possibly think of is finding all tags with find("tags", "*") (is * the wildcard?) and then dealing with duplicates in Java, but I don’t believe that’s the intended use.

Advertisement

Answer

You can use either of the two methods to get distinct tags from the collection test.

JavaScript

Assuming the Tags class is defined as follows and represents the Panache entity:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement