select within an element which might be array or string in rethinkdb

159 views Asked by At

My json in the rethinkdb database is as follows (I have given 4 docs as an example) :

    {
        "label": {
            "id": 59,
            "country": "Germany",
            "formats": {
                "format": {
                    "text": "",
                    "name": "CD",
                    "qty": 1,
                    "descriptions": {
                        "description": [
                            "Album",
                            "Limited Edition"
                        ]
                    }
                }
            }
        }
    }


    {
        "label": {
            "id": 60,
            "country": "US",
            "formats": {
                "format": {
                    "text": "",
                    "name": "CD",
                    "qty": 1,
                    "descriptions": {
                        "description": "Album"
                    }
                }
            }
        }
    }

    {
        "label": {
            "formats": {
                "format": {
                    "text": "",
                    "name": "Vinyl",
                    "qty": 1,
                    "descriptions": {
                        "description": [
                            "12\"",
                            "33 ⅓ RPM"
                        ]
                    }
                }
            },
            "country": "US",
            "id": 42
        }
    }
{
    "label": {
        "formats": {
            "format": {
                "text": "",
                "name": "Vinyl",
                "qty": 1,
                "descriptions": {
                    "description": "12\""
                }
            }
        },
        "country": "US",
        "id": 10
    }
}

I would like to filter those labels which are albums. The description tag contains this information. However, this element is sometimes an array, sometimes a string. I need those labels which contains the value "Album" regardless the data type. So far I can only get those values where "description" is a string. This is the code I can use so far:

r.table("labels")('label').filter(r.row("formats")("format")("descriptions")("description").eq("Album"))('id')

Is there a way to get even those id values where Album exists within an array as well? Thanks in advance

1

There are 1 answers

2
mlucy On BEST ANSWER

I would recommend changing your data format so that description is always an array, even if that array only has one element in it. If you do that, then this query works:

r.table('labels')('label').filter(function(row) {
  return row("formats")("format")("descriptions")("description").contains('Album');
})

If you don't want to do that, then you can do this:

r.table('labels')('label').filter(function(row) {
  return row("formats")("format")("descriptions")("description").do(function(desc) {
    return r.branch(desc.typeOf().eq('ARRAY'), desc.contains('Album'), desc.eq('Album'));
  });
})