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

Can't filter results using IN operator #4596

Closed
marcfrankel opened this issue May 25, 2022 · 2 comments
Closed

Can't filter results using IN operator #4596

marcfrankel opened this issue May 25, 2022 · 2 comments

Comments

@marcfrankel
Copy link

marcfrankel commented May 25, 2022

How frequently does the bug occur?

All the time

Description

Following the documentation, I'm trying to filter a query by a list of string primary ids I have. However, when using the IN operator as part of the query I can never seem to get it to work. Apologies if this is not actually a bug, but perhaps an example of the IN operator is due in the documentation if I'm just doing this wrong. I give an example below

Stacktrace & log output

[Unhandled promise rejection: Error: Exception in HostFunction: Unsupported comparison between type 'string' and type 'link']

Can you reproduce the bug?

Yes, always

Reproduction Steps

The error above is produced when I attempt to use the IN operator on any field (indexed, non-indexed, string, not string, primary key, etc).

const test = ['foo']; realm.objects('QueuedItem').filtered('mediaCategory IN $0', test)

Version

10.20.0-beta.5

What SDK flavour are you using?

Local Database only

Are you using encryption?

No, not using encryption

Platform OS and version(s)

iOS 15.5

Build environment

Expo

Cocoapods version

No response

@marcfrankel
Copy link
Author

@kraenhansen
Copy link
Member

Unfortunately, the IN operators right hand operand needs to be a list on a Realm object which is already in the database.
That is, unfortunately you cannot supply an array as the value to replace the placeholder.

As an alternative you can use the OR operator to achieve the same:

const realm = new Realm({ schema: [{ name: "Person", properties: { name: "string" } }] });
realm.write(() => {
  realm.create("Person", { name: "Alice" });
  realm.create("Person", { name: "Bob" });
  realm.create("Person", { name: "Charlie" });
});
const names = ["Alice", "Bob"];
const query = names.map((_, i) => "name = $" + i).join(" OR ");
const persons = realm.objects("Person").filtered(query, ...names);

Please see this issue for more information and upvote that if you want this feature too 👍 (closing this as a duplicate).

@kraenhansen kraenhansen closed this as not planned Won't fix, can't repro, duplicate, stale May 30, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
@sync-by-unito sync-by-unito bot closed this as completed 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

2 participants