【问题标题】:RethinkDB: How to do recursive joins on three tables?RethinkDB:​​如何在三个表上进行递归连接?
【发布时间】:2014-02-15 04:36:20
【问题描述】:

我正在使用 Python Flask 开发一个带有 JSON API 的平台。在某些情况下,我需要加入三个表。 How to join tables with a array of IDs 给了我一些指导,但我需要一个解决方案。

假设我们有三个用于消息传递应用程序的表。

  1. 帐户
  2. 对话
  3. 消息
  4. 消息阅读器

账户表sn-p

{
    "id": "account111",
    "name": "John Doe",
},

对话表sn-p

{
    "id": "conversation111",
    "to": ["account111", "account222", "account333"], // accounts who are participating the conversation
    "subject": "RethinkDB",
}

消息表sn-p

{
    "id": "message111",
    "text": "I love how RethinkDB does joins.",
    "from": "account111", // accounts who is the author of the message
    "conversation": "conversation111"
}

消息阅读器表 sn-p

{
    "id": "messagereader111",
    "message": "message111",
    "reader": "account111",
}

我的问题是“当我收到对 id="account111" 的帐户文档的获取请求时,获取以下文档的神奇查询是什么?”

{
    "id": "account111",
    "name": John Doe,
    "conversations": [            // 2) Join account table with conversations
        {
           "id": "conversation111",
           "name": "RethinkDB",
           "to": [                     // 3) Join conversations table with accounts
               {
                    "id": "account111",
                    "name": "John Doe",
               },
               {
                    "id": "account222",
                    "name": "Bobby Zoya",
               },
               {
                    "id": "account333",
                    "name": "Maya Bee",
               },
           ]
           "messages": [        // 4) Join conversations with messages
               {
                   "id": "message111",
                   "text": "I love how RethinkDB does joins.",
                   "from": {        // 5) Join messages with accounts
                        "id": "account111",
                        "name": "John Doe",
                   },
                   "message_readers": [
                       {
                           "name": "John Doe",
                           "id": "account111",
                       }
                   ],
               },
           ],
        },
    ],
}

任何指导或建议都会很棒。 JavaScript 或 Python 代码会很棒。

【问题讨论】:

    标签: rethinkdb rethinkdb-python


    【解决方案1】:

    我很难理解您想要什么(您有多个 id 为 111 的文档),但我认为这是您要查找的查询

    Python 查询:

    r.table("accounts").map(lambda account: 
        account.merge({
            "conversations": r.table("conversations").filter(lambda conversation: 
                conversation["to"].contains(account["id"])).coerce_to("array").map(lambda conversation:
                conversation.merge({
                    "to": conversation["to"].map(lambda account: 
                        r.table("accounts").get(account)).pluck(["id", "name",]).coerce_to("array"),
                    "messages": r.table("messages").filter(lambda message:
                        message["conversation"] == conversation["id"]).coerce_to("array").map(lambda message:
                        message.merge({
                            "from": r.table("accounts").get(message["from"]).pluck(["id", "name",]),
                            "readers": r.table("message_readers").filter(lambda message_reader:
                                message["id"] == message_reader["message"]).coerce_to("array").order_by(r.desc("created_on")),
                        })).order_by(r.desc("created_on"))
                })).order_by(r.desc("modified_on"))
        })).order_by("id").run(db_connection)
    

    【讨论】:

    • 我已经修改了 id 值,以便更容易理解问题。您能否确认解决方案与问题相符?
    • 我认为这应该是你要找的。​​span>
    • 你能解释一下索引的作用吗?为什么表叫“sn-ps”?
    • 顺便说一下,查询看起来很容易理解。我会测试它,看看它是否有效。
    • 我的错,我以为最后一个表是“sn-p”而不是“消息”。我刚刚更新了查询。关于索引,您可以在此处阅读有关索引的信息:rethinkdb.com/docs/secondary-indexes -- 我现在也删除了索引的使用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-06
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多