Realm-js: Support IN queries

Created on 21 May 2016  路  15Comments  路  Source: realm/realm-js

Let's say I have the following sample data:

[
  { id: 1, value: 'something' },
  { id: 2, value: 'something' },
  { id: 3, value: 'something' },
  { id: 4, value: 'something' },
  { id: 5, value: 'something' },
  { id: 6, value: 'something' },
  { id: 7, value: 'something' },
  { id: 8, value: 'something' },
  { id: 9, value: 'something' },
  { id: 10, value: 'something' },
]

When I execute the following:

let filtered1 = sample.filtered('id > 5')

it returns one Results object with 5 filtered values like this:

// value of filtered1 in console
Results {-1: undefined, Symbol(): 2, Symbol(): 8, Symbol(): "results"}

However, my question is how I can filter random ids? For example, I want to filter ids of 2, 4, 7, and 10.
What I can think of is:

let filtered2 = [2,4,7,10].map( (id) => {
   return sample.filtered('id == $0', id);
})

But the problem here is it returns an array of 4 Results objects, not the single Results object.

// value of filtered2 in console
[Results, Results, Results, Results]

Is it possible to get the results like the filtered1 in the second case?

O-Community Pipeline-Idea-Backlog T-Feature

Most helpful comment

Here is a code snippet that should generate the query you want to run:

var filtered = sample.filtered([2,4,7,10].map((id) => 'id == ' + id).join(' OR '));

This should create a query of the form id == 2 OR id == 4 OR id == 7 OR id ==10. Once we support IN queries it will do this for you internally.

All 15 comments

At some point we will support a syntax for this type of query. Something like:

let filtered = sample.filtered('id IN [2, 4, 7, 10]')

Unfortunately we don't yet support this so for now you will have to generate a query to do this yourself.

Here is a code snippet that should generate the query you want to run:

var filtered = sample.filtered([2,4,7,10].map((id) => 'id == ' + id).join(' OR '));

This should create a query of the form id == 2 OR id == 4 OR id == 7 OR id ==10. Once we support IN queries it will do this for you internally.

Thank you. It works with join. Maybe IN queries capability could be really helpful. Thanks again. 馃憤

Leaving this open until we add IN support.

Looking forward to see IN condition support in realm js :-)

@alazier Do you have roadmap for this feature yet?

@dzuncoi There are a number of query tasks we want to look into and hopefully soon, but no timeline yet, sorry.

@kristiandupont No problem, just want to know the timeline, I'm still fine with current query syntax :)

My collection contains strings, which I'd like to quote while building the query. Didn't find a quote() function. Is there one ?

@alazier Hello,
I believe the syntax for Realm-js has changed since your answer. How would I adapt your answer since now you have to write a query like so:

sample.filtered('Id == $0', someVariable)

To an array?
And is there any progress on an "IN" predicate?
Thank you.

Any updates for supporting IN queries?

@donni106
Try something like this:

import RealmQuery from 'realm-query';

let items = RealmQuery
                      .where(realm.objects('superlist'))
                      .in('Id', ids) //ids here being an array
                      .findAll();

Any update on IN operator?

Is it possible to make aggregation(joins) between two collections in realm Database?

Was this page helpful?
0 / 5 - 0 ratings