Realm-js: Queries on list of strings property

Created on 23 May 2018  Â·  12Comments  Â·  Source: realm/realm-js

Goals

filtered list of strings property

Code Sample

const BookrackSchema = {
    name: 'Bookrack',
    properties: {
        book_names:'string[]',
    }
};

Bookrack1.book_names=['A','B','C'];
Bookrack2.book_names=['B','C','D'];
Bookrack3.book_names=['C','D','E'];


a:realm.objects('Bookrack').filtered(`ANY book_names = $0 `,'B');
b:realm.objects('Bookrack').filtered(`book_names CONTAINS  $0 `,'B');

Expected Results

get Bookrack1 And Bookrack2

Actual Results

a.The keypath following 'ANY' or 'SOME' must contain a list
b.Object type 'Table' not supported

Steps to Reproduce

Version of Realm and Tooling

  • Realm JS SDK Version: ?"realm": "^2.3.0"
  • Node or React Native: ?node 8.4.0
  • Client OS & Version: ?mac 10.13.4
  • Which debugger for React Native: ?/None
O-Community Pipeline-Idea-Backlog T-Feature

Most helpful comment

It is currently not supported to query list of strings, only list of objects can be queried.

All 12 comments

It is currently not supported to query list of strings, only list of objects can be queried.

@kneth

It is currently not supported to query list of strings, only list of objects can be queried.

I think I have the same problem. Please, could you show me a workaround? Link to Stackoverflow

It is very important for me. I could donate a little money to you.

You can store the string values in a proxy object:

const StringValueSchema = {
    name: 'StringValue',
    properties: {
        value:'string',
    }
};

const BookrackSchema = {
    name: 'Bookrack',
    properties: {
        book_names:'StringValue[]',
    }
};

Then you can query like:

realm.objects('Bookrack').filtered(`ANY book_names.value = $0 `,'B');

@nirinchev
First, thank you so much for your response. I tried your example and i used the following for initializing:

       realm.write(() => {
          realm.create("Cars", {
            name: "Honda",
            category: "compact",
            image: global.base64_images.honda,
              specifications: ["test"],
          })

Now it returns me an error "Error: JS value must be of type "object", got (test).

for specifications i need a list of strings like: ["automatic,"suv","green"]. Excuse me.. do i miss something stupid?

That is correct - because we're faking a list of strings, you'll need to wrap them in objects:

realm.write(() => {
  realm.create("Cars", {
    name: "Honda",
    category: "compact",
    image: global.base64_images.honda,
    specifications: ["test"].map((str) => {
      return realm.create("StringValue", {
        value: str
      });
    }),
});

Similarly, if you want to get a list of strings from the collection, you'll need a reverse map:

const strings = car.specifications.map((s) => s.value);

@kneth I want ask that it is not supported to query list of strings, how about int or double? just strings?

@mingxin-yang Querying lists of primitive types (string, int, etc.) is still an open issue: https://github.com/realm/realm-object-store/issues/513

I hope that we can make some progress in the fall but I can't give you an ETA.

Hi I am also interested in the progress of this implementation

I think it's very necessary. At least, it should be explained that there is not support for query in list of primitive properties. I've already created a my db expecting that I could to such type of queries.
Also it would avoid to create a new class for each field when is as simple as a string or number.

Now I've got 2 options either change to a single string with a separator (no fun of this one) or create a class for each field (it adds more complexity to maintain the db)

This is in progress now in realm-core.

➤ Brian Munkholm commented:

[~kenneth.geisshirt] I assume this is done now and can be closed?

Realm Core has implemented comparison operators for array of primitive types, and it has been released in Realm JavaScript v10.0.0-beta.1. I suggest that you try out the latest beta (v10.0.0-beta.7).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timanglade picture timanglade  Â·  3Comments

laznrbfe picture laznrbfe  Â·  3Comments

texas697 picture texas697  Â·  3Comments

ashah888 picture ashah888  Â·  3Comments

jmartindivmedianet picture jmartindivmedianet  Â·  3Comments