select 在 rethinkdb 中可能是数组或字符串的元素中
select within an element which might be array or string in rethinkdb
我在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
}
}
我想过滤那些属于相册的标签。描述标签包含此信息。然而,这个元素有时是一个数组,有时是一个字符串。无论数据类型如何,我都需要那些包含值 "Album" 的标签。到目前为止,我只能获得 "description" 是字符串的那些值。这是我目前可以使用的代码:
r.table("labels")('label').filter(r.row("formats")("format")("descriptions")("description").eq("Album"))('id')
有没有办法获取数组中也存在 Album 的那些 id 值?提前致谢
我建议更改您的数据格式,使 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'));
});
})
我在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
}
}
我想过滤那些属于相册的标签。描述标签包含此信息。然而,这个元素有时是一个数组,有时是一个字符串。无论数据类型如何,我都需要那些包含值 "Album" 的标签。到目前为止,我只能获得 "description" 是字符串的那些值。这是我目前可以使用的代码:
r.table("labels")('label').filter(r.row("formats")("format")("descriptions")("description").eq("Album"))('id')
有没有办法获取数组中也存在 Album 的那些 id 值?提前致谢
我建议更改您的数据格式,使 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'));
});
})