首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >匹配字符串表达式的REQL

匹配字符串表达式的REQL
EN

Stack Overflow用户
提问于 2015-06-09 03:24:47
回答 1查看 207关注 0票数 0

我有以下json:

代码语言:javascript
运行
复制
{
"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"
}
}

我正在尝试得到那些以“黎明时分”结尾的标题。

我正在使用以下命令

代码语言:javascript
运行
复制
r.db("discogs1").table("releases").filter(function(doc){  return doc('release')('title').match('At Dawn$')})

但我得到的错误如下:

代码语言:javascript
运行
复制
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$"); })

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

EN

回答 1

Stack Overflow用户

发布于 2015-06-09 04:10:12

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

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

代码语言:javascript
运行
复制
r.db("discogs1").table("releases")
 .filter(r.row('release')('title').typeOf().ne('STRING'))
 .count()

理想情况下,结果应该是0,因为任何文档都不应该有不是字符串的title属性。如果它高于0,这就是为什么你会得到一个错误。

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

代码语言:javascript
运行
复制
r.db("discogs1").table("releases")
 .filter(r.row('release')('title').typeOf().eq('STRING'))
 .filter(function(doc){  return doc('release')('title').match('At Dawn$')})

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

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

代码语言:javascript
运行
复制
r.db("discogs1").table("releases")
 .filter(r.row('release')('title').typeOf().ne('STRING'))
 .merge(function (row)  {
   return {
      'title': row('title').coerceTo('string')
   }
 })

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

代码语言:javascript
运行
复制
r.db("discogs1").table("releases")
 .filter(r.row('release')('title').typeOf().ne('STRING'))
 .delete()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30717419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档