我有2个RethinkDB表:
Left:
{
id: String,
title: String,
key: String // for mapping with table Right
}
Right:
{
id: String
title: String
description: String
}
Run Code Online (Sandbox Code Playgroud)
RethinkDB有eqJoin()和zip()方法,它们帮助我们将表的所有字段克隆到表左边:
r.db("myDB").table("Left")
.eqJoin("key", r.db("myDB").table("Right"))
.zip()
Run Code Online (Sandbox Code Playgroud)
结果将如下所示:
[{
id: "the-id",
key: "Right-object-id",
title: "Title of Right Object",
description: "Description of Right Object"
// => title of Left Object was deleted
}]
Run Code Online (Sandbox Code Playgroud)
现在的问题是: 如何模拟像查询)填入(的猫鼬?
我希望结果看起来像这样:
[{
id: "the-id",
key: {
id: "Right-object-id"
title: "Title of Right Object"
description: "Description of Right Object"
}
}]
Run Code Online (Sandbox Code Playgroud)
使用eqJoin
您可以使用eqJoin以下内容映射结果:
r.db("myDB").table("Left")
.eqJoin("key", r.db("myDB").table("Right"))
.map(function (row){
return row('left').merge({ key: row('right') })
})
Run Code Online (Sandbox Code Playgroud)
子查询
虽然您可以eqJoin在RethinkDB 中使用,但子查询通常更易于使用且功能更强大.您可以使用该merge术语添加新密钥,然后使用子查询来设置该密钥的值:
r.db("myDB").table("Left")
.merge(function (row){
return {
'key': r.db("myDB").table("Right")).get(row('key'))
}
})
Run Code Online (Sandbox Code Playgroud)
我通常从不使用eqJoin.使用子查询并不容易.
| 归档时间: |
|
| 查看次数: |
266 次 |
| 最近记录: |