如何在 rethinkdb 中使用嵌入式链接加入
How to join using embedded links in rethinkdb
给定一个类似的架构:
[{
"id": 1,
"name": "Darth",
"family": [2,3]
},
{
"id": 2,
"name": "Luke"
},
{
"id": 3,
"name": "Leia"
}
]
我如何return Darth 和 outer 加入才能得到他所有的家人?
我想将 family
中的 ID 替换为家庭成员的文件,您可以用 get
做一个 map
。
像这样:
r.table('people').get(1) // Get Darth
// Add a `family` property to Darth
.merge(function (row) {
return {
// Map all the family member ids to their respective documents
'family': row('family').map(function (id) {
return r.table('people').get(id);
})
}
})
此查询的结果将是这样的:
{
"family": [
{
"id": 2 ,
"name": "Luke"
} ,
{
"id": 3 ,
"name": "Leia"
}
] ,
"id": 1 ,
"name": "Darth"
}
最终得到了下面的地图,我认为它比地图更高效。它将使用 id
上的默认索引
r.db('myDb')
.table('tests')
.get(1)
.merge(function (person) {
return {
myFamily: r.db('myDb').table('tests').getAll(r.args(person('family'))).coerceTo('ARRAY')
}
})
.pluck('name', 'myFamily');
给定一个类似的架构:
[{
"id": 1,
"name": "Darth",
"family": [2,3]
},
{
"id": 2,
"name": "Luke"
},
{
"id": 3,
"name": "Leia"
}
]
我如何return Darth 和 outer 加入才能得到他所有的家人?
我想将 family
中的 ID 替换为家庭成员的文件,您可以用 get
做一个 map
。
像这样:
r.table('people').get(1) // Get Darth
// Add a `family` property to Darth
.merge(function (row) {
return {
// Map all the family member ids to their respective documents
'family': row('family').map(function (id) {
return r.table('people').get(id);
})
}
})
此查询的结果将是这样的:
{
"family": [
{
"id": 2 ,
"name": "Luke"
} ,
{
"id": 3 ,
"name": "Leia"
}
] ,
"id": 1 ,
"name": "Darth"
}
最终得到了下面的地图,我认为它比地图更高效。它将使用 id
上的默认索引r.db('myDb')
.table('tests')
.get(1)
.merge(function (person) {
return {
myFamily: r.db('myDb').table('tests').getAll(r.args(person('family'))).coerceTo('ARRAY')
}
})
.pluck('name', 'myFamily');