0

I'm new to realm. How can I filter a nested schema in React Native Realm? I'm just trying to do it with Realm query. I can already handle this with javascript.

Models:

const ModelA = {
    name: "ModelA",
    primaryKey: "id",
    properties: {
        id: "objectId",
        name: "string",
        data: "ModelB[]"
    }
};

const ModelB = {
    name: "ModelB",
    properties: {
        label: "int",
        text: "string"
    }
};

I have such data.

[
    {
        "id": "6407d4949096750f578c536c",
        "name": "name1",
        "data": [
            { "label": "1", "text": "text1" },
            { "label": "1", "text": "text2" },
            { "label": "2", "text": "text3" },
            { "label": "2", "text": "text4" },
        ]
    },
    {
        "id": "7407d4949096750f578c536c",
        "name": "name2",
        "data": [
            { "label": "1", "text": "text1" },
            { "label": "2", "text": "text2" },
            { "label": "2", "text": "text3" },
            { "label": "2", "text": "text4" },
        ]
    }
]

Here I want to filter and list like label == 1. Just like in this example. How can I do that?

let list = realm.objects("ModelA").filtered(/* ? */);

console.log(JSON.stringify(list));

Console output:

[
    {
        "id": "6407d4949096750f578c536c",
        "name": "name1",
        "data": [
            { "label": "1", "text": "text1" },
            { "label": "1", "text": "text2" },
        ]
    },
    {
        "id": "7407d4949096750f578c536c",
        "name": "name2",
        "data": [
            { "label": "1", "text": "text1" },
        ]
    },
]
Blogger Klik
  • 160
  • 1
  • 2
  • 12
  • Does this answer your question? [get specfic objects from array inside array of object express js mongodb](https://stackoverflow.com/questions/75637971/get-specfic-objects-from-array-inside-array-of-object-express-js-mongodb) – Tobok Sitanggang Mar 10 '23 at 01:56
  • @TobokSitanggang no. – Blogger Klik Mar 10 '23 at 14:42
  • The question is a bit unclear and we are not a code-writing service; please include the code you've attempted so we understand more specifically what you're trying to do. Even your java query (which works?) may be useful. Is the goal to filter and return all `ModalA` objects that have `ModelB` property `label` equal to "1"? See [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Jay Mar 12 '23 at 14:10
  • @Jay `realm.objects("ModelA").filtered('data.label == 1')` This is the code I wrote. The question is pretty clear. I'm just trying to get the output stated at the bottom in the question with the realm query. (Not with Javascript. Only with Realm query!!!) And I guess realm doesn't support it. – Blogger Klik Mar 12 '23 at 15:07
  • Good. Looks like you are querying a list - but remember; only you know your app and code, we can only work with the data you give us. Just because it's clear to you doesn't mean it's clear to us, right? Realm fully supports queries of that fashion and we do it all the time. Why can't you do that? Do you have some example Realm code you've attempted that doesn't work? Doesn't [this answer](https://stackoverflow.com/questions/39944435/realm-with-react-native-how-to-query-through-list-objects) do that? Please update your question with the query you've attempted and we'll take a look! – Jay Mar 12 '23 at 15:27
  • Or [this answer](https://stackoverflow.com/questions/39671131/use-react-native-realm-to-query-through-multiple-list-objects) or even [this one](https://stackoverflow.com/questions/43193432/realm-js-querying-a-string-inside-a-list-of-string-mongo-equivalent-to-elemma) – Jay Mar 12 '23 at 15:29
  • @Jay I updated the question. Can you understand now? – Blogger Klik Mar 12 '23 at 15:47
  • Thank you for clarifying the question - answer posted. – Jay Mar 12 '23 at 16:08

2 Answers2

0

following the documentation and tutorial-query-language

you should be able to filter it using filtered.

that would be like:

const filterData = realm.objects('Model');
const result = filterData.filtered( 'label == $0',  '1');
Tobok Sitanggang
  • 607
  • 5
  • 15
0

Restating the question: There are two objects in Realm; ModelA and ModelB

ModelA has a List property data containing ModelB objects

ModelB has label property

The objective is to query ModelA objects for any that have a ModelB list of data where label property == 1

Here's the query

let results = realm.objects("ModelA").filtered("ANY data.label == '1'")

When that query is run against the original data, the output matches the output in the question, for the top level objects

[
    {
        "id": "6407d4949096750f578c536c",
        "name": "name1",
        "data": [
            { "label": "1", "text": "text1" },
            { "label": "1", "text": "text2" },
            { "label": "2", "text": "text3" },
            { "label": "2", "text": "text4" },
        ]
    },
    {
        "id": "7407d4949096750f578c536c",
        "name": "name2",
        "data": [
            { "label": "1", "text": "text1" },
            { "label": "2", "text": "text2" },
            { "label": "2", "text": "text3" },
            { "label": "2", "text": "text4" },
        ]
    }
]

Keeping in mind that since we are querying on the top ModelA objects, those entire objects are returned. Filtering is only 1 level deep, and returns the entire object e.g. it won't filter out related objects, that would either require additional in-code filtering or reversing how the filter is done; filter on the ModelB objects that are related to ModelA objects.

Jay
  • 34,438
  • 18
  • 52
  • 81
  • `filter()` ? I think you tried to write `filtered()` I'm glad you finally understood my question. Like I said, Realm doesn't support it. – Blogger Klik Mar 12 '23 at 18:02
  • @BloggerKlik I am not sure what Realm doesn't support. The query is on the ModelA objects and that's what it returns. Are you expecting it to return something else other than the ModelA object? Do you perhaps want to return ModelB objects? If so, adding a LinkingObjects property makes that a fairly straightforward query - with that you get back the ModelB objects and via the reverse graph, have access to the ModelA object as well. – Jay Mar 12 '23 at 19:08