包括couchdb中的所有引用文档

时间:2016-09-27 06:57:43

标签: couchdb

现在我通过发出几乎所有内容然后在node.js服务器中组合输出来获得所需的输出。

问题在于,由于我无法从poll文档访问组文档以发出组成员,因此我必须发出每个组及其成员/ created_by。

我想知道这是否可能,还是我需要重新构建我的文件或什么?

无论如何,这些是示例文档和所需的输出。

poll1:

{
   "_id": "0166801aff2aca04894bbd42b200003a",
   "doc_type": "poll",
   "question": "What to do?",
   "choices": [
       {
           "_id": "530d40abfb606d0fc12131e3b1b51590",
           "text": "Nothing",
           "votes": [
               "ef29b82702be83123837aefd001c46e9"
           ],
           "created_by": "ef29b82702be83123837aefd001c46e9"
       },
       {
           "_id": "f01c25231cef239b84a1a25bcbf0eb1b",
           "text": "Everything",
           "votes": [
               "ef29b82702be83123837aefd001c46e9",
               "967cb55f3fbb27a25c45b5e7d8cecd4f"
           ],
           "created_by": "ef29b82702be83123837aefd001c46e9"
       }
   ],
   "created_by": "ef29b82702be83123837aefd001c46e9",
   "expire_date": "1475535772",
   "groups": [
       "d6cdb694358b81ac0bcfa8574d7d6c72"
   ]
}

组1:

{
   "_id": "d6cdb694358b81ac0bcfa8574d7d6c72",
   "doc_type": "group",
   "name": "Friends",
   "members": [
       "ef29b82702be83123837aefd001c46e9"
   ],
   "created_by": "967cb55f3fbb27a25c45b5e7d8cecd4f"
}

用户1:

{
   "_id": "967cb55f3fbb27a25c45b5e7d8cecd4f",
   "doc_type": "user",
   "uuid": "d272c0bc-9f6d-42f9-9503-0e11735f7798",
   "username": "Peter"
}

用户2:

{
   "_id": "ef29b82702be83123837aefd001c46e9",
   "doc_type": "user",
   "uuid": "40a818e8-0f11-4832-9a18-erd3d85b88b0",
   "username": "Thomas"
}

所需的输出:

{
    "_id": "0166801aff2aca04894bbd42b200003a",
    "doc_type": "poll",
    "question": "What to do?",
    "choices": [
        {
            "_id": "530d40abfb606d0fc12131e3b1b51590",
            "text": "Nothing",
            "votes": [
                {
                    "_id": "ef29b82702be83123837aefd001c46e9",
                    "doc_type": "user",
                    "uuid": "40a818e8-0f11-4832-9a18-erd3d85b88b0",
                    "username": "Thomas"
                }
            ],
            "created_by": {
                "_id": "ef29b82702be83123837aefd001c46e9",
                "doc_type": "user",
                "uuid": "40a818e8-0f11-4832-9a18-erd3d85b88b0",
                "username": "Thomas"
            }
        },
        {
            "_id": "f01c25231cef239b84a1a25bcbf0eb1b",
            "text": "Everything",
            "votes": [
                {
                    "_id": "ef29b82702be83123837aefd001c46e9",
                    "doc_type": "user",
                    "uuid": "40a818e8-0f11-4832-9a18-erd3d85b88b0",
                    "username": "Thomas"
                },
                {
                    "_id": "967cb55f3fbb27a25c45b5e7d8cecd4f",
                    "doc_type": "user",
                    "uuid": "d272c0bc-9f6d-42f9-9503-0e11735f7798",
                    "username": "Peter"
                }
            ],
            "created_by": {
                "_id": "ef29b82702be83123837aefd001c46e9",
                "doc_type": "user",
                "uuid": "40a818e8-0f11-4832-9a18-erd3d85b88b0",
                "username": "Thomas"
            }
        }
    ],
    "created_by": {
        "_id": "ef29b82702be83123837aefd001c46e9",
        "doc_type": "user",
        "uuid": "40a818e8-0f11-4832-9a18-erd3d85b88b0",
        "username": "Thomas"
    },
    "expire_date": "1475535772",
    "groups": [
        {
            "_id": "d6cdb694358b81ac0bcfa8574d7d6c72",
            "doc_type": "group",
            "name": "Friends",
            "members": [
                {
                    "_id": "ef29b82702be83123837aefd001c46e9",
                    "doc_type": "user",
                    "uuid": "40a818e8-0f11-4832-9a18-erd3d85b88b0",
                    "username": "Thomas"
                }
            ],
            "created_by": {
                "_id": "967cb55f3fbb27a25c45b5e7d8cecd4f",
                "doc_type": "user",
                "uuid": "d272c0bc-9f6d-42f9-9503-0e11735f7798",
                "username": "Peter"
            }
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

您可以使用linked documentations从视图中获取群组,但无法获得用户。您需要查询另一个时间。

如果要使用单个查询,可以将其他元信息存储到poll文档中。例如,您可以让组具有用户ID。

{
  "groups": [
    {
      "groupId": "enter_your_group_id_here",
      "usersIds": [
        "user1",
        "user2",
        "user3"
      ]
    }
  ]
}