0

我在 rethinkdb 数据库中的 json 如下(我给出了 4 个文档作为示例):

    {
        "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
    }
}

我想过滤那些专辑标签。描述标签包含此信息。然而,这个元素有时是一个数组,有时是一个字符串。无论数据类型如何,我都需要那些包含值“专辑”的标签。到目前为止,我只能得到“描述”是字符串的那些值。这是我到目前为止可以使用的代码:

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

有没有办法获得即使是专辑也存在于数组中的那些 id 值?提前致谢

4

1 回答 1

0

我建议更改您的数据格式,使其description始终是一个数组,即使该数组中只有一个元素。如果您这样做,则此查询有效:

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

如果你不想这样做,那么你可以这样做:

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'));
  });
})
于 2015-06-17T02:29:32.550 回答