RethinkDB eqJoin + MAP
RethinkDB eqJoin + MAP
我在使用 RethinkDB 时遇到了这个问题。
我有 3 个文档:
个人(account字段为二级id):
{
"account": "3580654" ,
"id": "356ee84a-8eb5-4b9e-8af5-1cf1ebfe556d" ,
"name": "Jesus",
"lastname": "Omilon"
}
位置(account字段为二级id):
{
"account": "3580654" ,
"id": "53d02b06-8fe2-4d48-a8ab-ffd2ff519a32" ,
"ip": "127.0.0.1",
"state": "21"
}
Phones(account字段是一个secondary id):
{
"account": "3580654" ,
"id": "692e7e5f-85f2-444f-b6de-a2b1136b80fa" ,
"phone": "555-444.44.44",
"state": "21"
},
{
"account": "3580654" ,
"id": "079579aa-ece9-408f-9d00-889a1cb56fc4" ,
"phone": "555-432.33.33",
"state": "21"
}
所以我想加入这样的查询:
{
"account": "3580654",
"id": "356ee84a-8eb5-4b9e-8af5-1cf1ebfe556d",
"name": "Jesus",
"lastname": "Omilon",
"state": 21,
"ip": "127.0.0.1",
"phones": ["555-444.44.44", "555-432.33.33"]
}
谁能帮我做这个?我尝试使用 eqJoin 和地图,但没有成功。
感谢您的帮助。
感谢您提供非常详细的样本数据。这有助于更轻松地回答问题。
因为 person
和 location
可以合并以创建最终位置。我们可以使用 eqJoin
来连接数据。
phone
是一个数组,所以我们可以从上面的结果运行一个map
,在map
函数里面,我们查询[=35的额外数据=] table 并与主文档合并。
最终查询如下所示:
r.table('Person').eqJoin('account', r.table('Location'), {index: 'account'}).zip()
.map(function(doc) {
return doc.merge({
'phone': r.table('Phones').getAll(doc('account'), {index: 'account'})('phone').coerceTo('ARRAY')
})
})
它产生结果:
{
"account": "3580654",
"id": "53d02b06-8fe2-4d48-a8ab-ffd2ff519a32",
"ip": "127.0.0.1",
"lastname": "Omilon",
"name": "Jesus",
"phone": [
"555-444.44.44",
"555-432.33.33"
],
"state": "21"
}
注意这里调用zip()
时Person
和Location
的id
字段有冲突。因此,您可以稍微调整查询以保留您喜欢的 id
字段
我在使用 RethinkDB 时遇到了这个问题。
我有 3 个文档:
个人(account字段为二级id):
{
"account": "3580654" ,
"id": "356ee84a-8eb5-4b9e-8af5-1cf1ebfe556d" ,
"name": "Jesus",
"lastname": "Omilon"
}
位置(account字段为二级id):
{
"account": "3580654" ,
"id": "53d02b06-8fe2-4d48-a8ab-ffd2ff519a32" ,
"ip": "127.0.0.1",
"state": "21"
}
Phones(account字段是一个secondary id):
{
"account": "3580654" ,
"id": "692e7e5f-85f2-444f-b6de-a2b1136b80fa" ,
"phone": "555-444.44.44",
"state": "21"
},
{
"account": "3580654" ,
"id": "079579aa-ece9-408f-9d00-889a1cb56fc4" ,
"phone": "555-432.33.33",
"state": "21"
}
所以我想加入这样的查询:
{
"account": "3580654",
"id": "356ee84a-8eb5-4b9e-8af5-1cf1ebfe556d",
"name": "Jesus",
"lastname": "Omilon",
"state": 21,
"ip": "127.0.0.1",
"phones": ["555-444.44.44", "555-432.33.33"]
}
谁能帮我做这个?我尝试使用 eqJoin 和地图,但没有成功。
感谢您的帮助。
感谢您提供非常详细的样本数据。这有助于更轻松地回答问题。
因为 person
和 location
可以合并以创建最终位置。我们可以使用 eqJoin
来连接数据。
phone
是一个数组,所以我们可以从上面的结果运行一个map
,在map
函数里面,我们查询[=35的额外数据=] table 并与主文档合并。
最终查询如下所示:
r.table('Person').eqJoin('account', r.table('Location'), {index: 'account'}).zip()
.map(function(doc) {
return doc.merge({
'phone': r.table('Phones').getAll(doc('account'), {index: 'account'})('phone').coerceTo('ARRAY')
})
})
它产生结果:
{
"account": "3580654",
"id": "53d02b06-8fe2-4d48-a8ab-ffd2ff519a32",
"ip": "127.0.0.1",
"lastname": "Omilon",
"name": "Jesus",
"phone": [
"555-444.44.44",
"555-432.33.33"
],
"state": "21"
}
注意这里调用zip()
时Person
和Location
的id
字段有冲突。因此,您可以稍微调整查询以保留您喜欢的 id
字段