REQL匹配字符串表达式

时间:2015-06-08 19:24:47

标签: rethinkdb reql

我有以下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"
}
}

我正试图让那些以'At Dawn'结尾的头衔。

我正在使用以下命令

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

但我得到的错误如下:

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$"); })

我尝试了不同的组合,但我似乎无法让它工作

1 个答案:

答案 0 :(得分:0)

您的某些文档似乎没有row('release')('title')属性字符串。其中一些是数字,因此当您尝试对它们调用.match时,它们会抛出错误,因为.match仅适用于字符串。

要查看是否属实,请尝试以下操作:

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

理想情况下,结果应为0,因为没有文档应该具有title属性,而不是字符串。如果它高于0,那就是您收到错误的原因。

如果您只想获取title是字符串的文档,您可以执行以下操作:

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

此查询将起作用,因为它将过滤我们标题不是字符串的所有文档。

如果您想将所有标题强制转换为字符串,您可以执行以下操作:

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

如果要删除标题不是字符串的所有文档,可以执行以下操作:

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