Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support IN queries #450

Closed
sunglee opened this issue May 20, 2016 · 15 comments
Closed

Support IN queries #450

sunglee opened this issue May 20, 2016 · 15 comments

Comments

@sunglee
Copy link

sunglee commented May 20, 2016

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?

@alazier
Copy link
Contributor

alazier commented May 20, 2016

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.

@alazier alazier changed the title Filtering random values Support IN queries May 20, 2016
@alazier
Copy link
Contributor

alazier commented May 20, 2016

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.

@sunglee
Copy link
Author

sunglee commented May 23, 2016

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

@alazier alazier closed this as completed May 23, 2016
@alazier alazier reopened this May 24, 2016
@alazier
Copy link
Contributor

alazier commented May 24, 2016

Leaving this open until we add IN support.

@Arman92
Copy link

Arman92 commented Dec 7, 2016

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

@dzuncoi
Copy link

dzuncoi commented Mar 6, 2017

@alazier Do you have roadmap for this feature yet?

@kristiandupont
Copy link
Contributor

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

@dzuncoi
Copy link

dzuncoi commented Mar 8, 2017

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

@stephanejais
Copy link

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

@HZSamir
Copy link

HZSamir commented Dec 28, 2017

@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.

@donni106
Copy link

donni106 commented May 8, 2018

Any updates for supporting IN queries?

@HZSamir
Copy link

HZSamir commented May 9, 2018

@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();

@juanfer0002
Copy link

Any update on IN operator?

@kneth
Copy link
Contributor

kneth commented Feb 25, 2020

Tracked in realm/realm-core#2978

@abhishekmatta999
Copy link

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

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests