2

Background

Framework: React Native

Library: Realm JS

I am writing a query to search whether an ID is included in a collection

following the official doc in https://static.realm.io/downloads/files/NSPredicateCheatsheet.pdf

Left hand expression must appear in collection specified by right hand expression. i.e. name IN {‘Milk’, ‘Eggs’, ‘Bread’}

Problem

When applying this query .filtered('subcategoryId IN {"a","b"}')

Render Error: Invalid predicate: 'subcategoryId IN {"a","b"}: syntax error, unexpected '{'

Error message

I've tried different syntax on the right side collection, still doesn't work.

Anyone has similar experience?

tony1233465
  • 37
  • 1
  • 6
  • Perhaps it wants an array `[` and not an object `{`? – mardubbles Apr 16 '22 at 06:34
  • I've tried this one, same error: `Error: Invalid predicate: 'subcategoryId IN ["a","b"]': syntax error, unexpected '['` – tony1233465 Apr 16 '22 at 06:35
  • Well it would be better to revert to `[` so debugging can take place. Is `subcategoryId` defined? – mardubbles Apr 16 '22 at 06:36
  • I have no idea what system you’re using there, but Mongo queries look like `{subcategoryId: {$in: ['a', 'b']}}`…‽ – deceze Apr 16 '22 at 06:38
  • Seems like @deceze has the cure. Try it out, then update your question with other issues. – mardubbles Apr 16 '22 at 06:39
  • It is a query system in realm for JS: https://www.mongodb.com/docs/realm-sdks/js/latest/tutorial-query-language.html – tony1233465 Apr 16 '22 at 06:40
  • so it should follow syntax like: `id == 'ab`, `date < 'xx'` – tony1233465 Apr 16 '22 at 06:48
  • Seems like you dont include your attempt on "so it should follow syntax like id == ab, date < xx" in your question? Which you can edit (your question). – mardubbles Apr 16 '22 at 08:21
  • It’s a good idea to include code, errors and and structures as *text*, images. That way, if they are needed in an answer, they can be copied and pasted. Also, images are not searchable which may prevent future readers from locating the question. Take a look at [No Images Please](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) – Jay Apr 16 '22 at 13:54
  • That cheatsheet is the NSPredicate Swift SDK and is somewhat outdated - we now have type safe filters for example. Please review [Realm Query Language](https://www.mongodb.com/docs/realm/reference/realm-query-language/#std-label-realm-query-language) for the React Native SDK. And see [this github](https://github.com/realm/realm-js/issues/2781) and [this github](https://github.com/realm/realm-js/issues/450) noting that IN is now supported. – Jay Apr 16 '22 at 14:19
  • Thanks for your answers, but I couldn't find any solution works. do u guys have any example about IN query that works? – tony1233465 Apr 17 '22 at 12:19
  • 1
    Research is your friend! See [this](https://github.com/realm/realm-core/issues/4266) and *We only support IN when the right hand side is a list property* and then an alternate solution [here](https://github.com/realm/realm-js/issues/2781#issuecomment-607213640) – Jay Apr 17 '22 at 13:23
  • I've read so much doc but not much explanation on "IN" query. I thought "IN" is supported recently. so I assume "IN" query is not fully supported in Realm JS from your 2nd link even though they mentioned it in official doc. Thanks for your help! – tony1233465 Apr 17 '22 at 13:25

0 Answers0