REQL to match string expression

213 views Asked by At

I have the following json:

{
"release": {
    "genres": {
        "genre": "Electronic"
    },
    "identifiers": {
        "identifier": [
            {
                "description": "Text",
                "value": "5 709498 101026",
                "type": "Barcode"
            },
            {
                "description": "String",
                "value": 5709498101026,
                "type": "Barcode"
            }
        ]
    },
    "status": "Accepted",
    "videos": {
        "video": [
            {
                "title": "Future 3 - Renaldo",
                "duration": 446,
                "description": "Future 3 - Renaldo",
                "src": "http://www.youtube.com/watch?v=hpc9aQpnUjc",
                "embed": true
            },
            {
                "title": "Future 3 - Silver M from album We are the Future / 1995 Denmark / Archivos de Kraftwerkmusik",
                "duration": 461,
                "description": "Future 3 - Silver M from album We are the Future / 1995 Denmark / Archivos de Kraftwerkmusik",
                "src": "http://www.youtube.com/watch?v=nlcHRI8iV4g",
                "embed": true
            },
            {
                "title": "Future 3 - Bubbles At Dawn",
                "duration": 710,
                "description": "Future 3 - Bubbles At Dawn",
                "src": "http://www.youtube.com/watch?v=ABBCyvGMOFw",
                "embed": true
            }
        ]
    },
    "labels": {
        "label": {
            "catno": "APR 010CD",
            "name": "April Records"
        }
    },
    "companies": {
        "company": {
            "id": 26184,
            "catno": "",
            "name": "Voices Of Wonder",
            "entity_type_name": "Published By",
            "resource_url": "http://api.discogs.com/labels/26184",
            "entity_type": 21
        }
    },
    "styles": {
        "style": [
            "Abstract",
            "IDM",
            "Downtempo"
        ]
    },
    "formats": {
        "format": {
            "text": "",
            "name": "CD",
            "qty": 1,
            "descriptions": {
                "description": "Album"
            }
        }
    },
    "country": "Denmark",
    "id": 5375,
    "released": "1995-00-00",
    "artists": {
        "artist": {
            "id": 5139,
            "anv": "",
            "name": "Future 3",
            "role": "",
            "tracks": "",
            "join": ""
        }
    },
    "title": "We Are The Future 3",
    "master_id": 638422,
    "tracklist": {
        "track": [
            {
                "position": 1,
                "duration": "8:04",
                "title": "Future 3"
            },
            {
                "position": 2,
                "duration": "7:38",
                "title": "Silver M"
            },
            {
                "position": 3,
                "duration": "7:27",
                "title": "Renaldo"
            },
            {
                "position": 4,
                "duration": "6:04",
                "title": "B.O.Y.D."
            },
            {
                "position": 5,
                "duration": "6:12",
                "title": "Fumble"
            },
            {
                "position": 6,
                "duration": "6:12",
                "title": "Dawn"
            },
            {
                "position": 7,
                "duration": "11:54",
                "title": "Bubbles At Dawn"
            },
            {
                "position": 8,
                "duration": "6:03",
                "title": "D.A.W.N. At 6"
            },
            {
                "position": 9,
                "duration": "8:50",
                "title": 4684351684651
            }
        ]
    },
    "data_quality": "Needs Vote",
    "extraartists": {
        "artist": [
            {
                "id": 2647642,
                "anv": "",
                "name": "Danesadwork",
                "role": "Cover",
                "tracks": "",
                "join": ""
            },
            {
                "id": 2647647,
                "anv": "",
                "name": "Djon Edvard Petersen",
                "role": "Photography By",
                "tracks": "",
                "join": ""
            },
            {
                "id": 114164,
                "anv": "",
                "name": "Anders Remmer",
                "role": "Written-By",
                "tracks": "",
                "join": ""
            },
            {
                "id": 435979,
                "anv": "",
                "name": "Jesper Skaaning",
                "role": "Written-By",
                "tracks": "",
                "join": ""
            },
            {
                "id": 15691,
                "anv": "",
                "name": "Thomas Knak",
                "role": "Written-By",
                "tracks": "",
                "join": ""
            }
        ]
    },
    "notes": "© 1995 April Records APS ℗ 1995 April Records APS"
}
}

I am trying to get those titles which end with 'At Dawn'.

I am using the following command

r.db("discogs1").table("releases").filter(function(doc){  return doc('release')('title').match('At Dawn$')})

But I get errors as follows:

RqlRuntimeError: Expected type STRING but found NUMBER in:r.db("discogs1").table("releases").filter(function(var_24) { return var_24("release")("title").match("At Dawn$"); })

I tried different combinations but I can't seem to get it to work

1

There are 1 answers

1
Jorge Silva On

It seems that some of your documents don't have a row('release')('title') property that is a string. Some of them are numbers, so when you try to call .match on them, they throw an error because .match only works on strings.

To see if this is true, try the following:

r.db("discogs1").table("releases")
 .filter(r.row('release')('title').typeOf().ne('STRING'))
 .count()

Ideally, the result of this should be 0, since no document should have a title property that's not a string. If it's higher than 0, that's why you're getting an error.

If you want to only get documents where the title is a string, you can do the following:

r.db("discogs1").table("releases")
 .filter(r.row('release')('title').typeOf().eq('STRING'))
 .filter(function(doc){  return doc('release')('title').match('At Dawn$')})

This query will work, because it will filter our all documents where the title is not a string.

If you want to coerce all title into strings, you can do the following:

r.db("discogs1").table("releases")
 .filter(r.row('release')('title').typeOf().ne('STRING'))
 .merge(function (row)  {
   return {
      'title': row('title').coerceTo('string')
   }
 })

If you want to delete all documents where the title is not a string, you can do this:

r.db("discogs1").table("releases")
 .filter(r.row('release')('title').typeOf().ne('STRING'))
 .delete()