Querying nested objects with relationships in realm react native


#1

I am trying to use realm DB in my project. here the schemas:

export const AllParentObj = {
    name:"AllParentObj",
    properties:{
        AllObjects:"OneChildObj[]" 
    }
}
export const OneChildObj = {
    name:"OneChildObj",
    properties:{
        chKey:"string",
        chItems:"subChild[]"
    }
};

export const subChild = {
    name:"subChild",
    properties:{
        subChName:"string",
        subChMsg:"string"
    }
};

and here is the way i am trying to read from realm:

let myCode = "langid1";
let myid="xyz";
let allObjs = this.DBRealm.objects('AllParentObj').
                    filtered(`AllObjs.chkey== '${myCode}' AND \
                                AllObjs.chItems.subChName== '${myid}'`);
the above query returning all the results of query AllObjs.chkey== '${myCode}'

the other condition is not taken for filtering. Is there any place i am making mistake in the above code?

i want the output from my filter to be the matching page inside a given lang code. ex: sample input json in my realm DB:

{
    "AllMessages":[
        {
            locale:"langid1",
            pages:[
                {
                    "pageName":"pagename",
                    "messages":
                        {
                            "ml1p1m1":"msgText",
                            "ml1p1m2":"ml1 m2text"
                        }

                },
                {
                    "pageName":"xyz",
                    "messages":
                        {
                            "ml1p2m1":"ml1p2m1 text",
                            "ml1p2m2":"ml1p2m2 text"
                        }
                },
                {
                    "pageName":"rtyui",
                    "messages":
                        {
                            "ml1p2m1":"ml1p3m1 text",
                            "ml1p2m2":"ml1p3m2 text"
                        }
                },

            ]
        },
        {
            locale:"langid2",
            pages:[
                {
                    "pageName":"abcd",
                    "messages":{
                            "ml2p1m1":"ml2p1m1 text",
                            "ml2p1m2":"ml2p1m2 text"
                        }
                },
                {
                    "pageName":"hjk",
                    "messages":{
                            "ml2p2m1":"ml2p2m1"
                        }
                },

            ]
        },

    ]
}

Output: for langid1 and pageName: xyz

{
        "pageName":"xyz",
        "messages":
        {
            "ml1p2m1":"ml1p2m1 text",
            "ml1p2m2":"ml1p2m2 text"
        }
}

can somebody please help? PS: there are not syntax errors. basically multiple filters are not getting applied. when i apply single filter even on a subChild level its working.


#2

Your query applies the “AND” at the top level - this will return results for any object which has items in it’s list which satisfy both conditions, but it could be that each condition is met on different items in the list. It looks like this is not what you intend.
In order to restrict the comparison to properties of the same item in a list, you can use a subquery.
See if this works:

let allObjs = this.DBRealm.objects('AllParentObj').
                    filtered(`SUBQUERY(AllObjs, $x, $x.chkey == '${myCode}' AND \
                                $x.chItems.subChName == '${myid}')[email protected] > 0`);

There’s some documentation about subqueries here and here.