RQL: DISTINCT in React Native

Hello, I am using @realm/react in my react native project. Is it possible to use DISTINCT in order to return unique field values from documents inside a collection? If yes, can you provide a simple example or point to documentation? I wasn’t able to find anything on my own.

Isn’t it something like this?

const uniqueDogs = dogs.filtered('name == $0 DISTINCT(name)', value);

and this may help

@Jay , That does work, however, I can’t get it to work without additional unwanted query param name == $0 . Just dogs.filtered(' DISTINCT(name)'); doesn’t work, throws an error:

Error: Exception in HostFunction: Invalid predicate: ' DISTINCT(report_type)': syntax error, unexpected '('
at node_modules\react-native\Libraries\Core\ExceptionsManager.js:null in reportException
at node_modules\react-native\Libraries\Core\ExceptionsManager.js:null in handleException
at node_modules\react-native\Libraries\Core\setUpErrorHandling.js:null in handleError
at node_modules\expo-dev-launcher\build\DevLauncherErrorManager.js:null in errorHandler
at node_modules\expo-dev-launcher\build\DevLauncherErrorManager.js:null in <anonymous>
at node_modules\@react-native\polyfills\error-guard.js:null in ErrorUtils.reportFatalError
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:null in __guard
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:null in callFunctionReturnFlushedQueue

Hmm. That’s a parameterized query. When using DISTINCT inline, it cannot operate independently and must be attached to at least one query filter, hence the name == $0

If I remember correctly, I believe this was implemented at some point

const uniqueDogs = realm.where(DogClass.class).distinct("name")

or it could be

.distinct("name").findAll

Documentation lacks solid examples so I put a documentation request in.

As mentioned, React is not my strong suit so maybe someone else can chime in.

1 Like