我有以下文件:
{ _id: "123", type: "project", worksite_id: "worksite_1" }
{ _id: "456", type: "document", project_id: "123" }
{ _id: "789", type: "signature", document_id: "456" }
我的目标是运行查询,并不可避免地对与worksite_id: worksite_1
有联系的所有文档进行过滤复制。
示例:
如果我希望从该工作站点获取所有内容,我应该能够检索所有这些文档。
通常,我只需要在worksite_id
和type:document
中添加一个type:signature
。但是,由于各种原因,工作地点可以在项目中更改。
我想知道是否可以创建索引或做一些我不打算显示的相似之处。
This感觉像是在正确的道路上,但说明将文档放在其他文档中,我只希望它们分开。
任何帮助将不胜感激。
答案 0 :(得分:2)
地图功能一次只考虑一个文档,因此除非该文档了解其他文档,否则无法将它们链接在一起。您的结构意味着用SQL术语进行三表联接。
根据您的结构,您所希望的最好是两个请求的解决方案。您可以创建仅显示签名文档的视图:
function (doc) {
if (doc && doc.type && doc.type === "signature" && doc.document_id) {
emit(doc.document_id, {_id: doc.document_id})
}
}
并使用相同的技术将项目链接到文档-但是您无法将所有三个链接在一起。
答案 1 :(得分:0)
我想我有您要的东西。
以下是一些数据:
{
"docs": [
{
"_id": "123",
"type": "project",
"code": "p001"
},
{
"_id": "1234",
"type": "worksitelog",
"documents": [
{
"timestamp": "20180921091501",
"project_id": "123",
"document_id": "457",
"signature_id": "789"
},
{
"timestamp": "20180921091502",
"project_id": "123",
"document_id": "457",
"signature_id": "791"
},
{
"timestamp": "20180921091502",
"project_id": "123",
"document_id": "458",
"signature_id": "791"
},
{
"timestamp": "20180921091502",
"project_id": "123",
"document_id": "456",
"signature_id": "790"
}
],
"worksite_id": "worksite_2"
},
{
"_id": "1235",
"type": "worksitelog",
"documents": [
{
"timestamp": "20180913101502",
"project_id": "125",
"document_id": "459",
"signature_id": "790"
}
],
"worksite_id": "worksite_1"
},
{
"_id": "124",
"type": "project",
"code": "p002"
},
{
"_id": "125",
"type": "project",
"code": "p003"
},
{
"_id": "456",
"type": "document",
"code": "d001",
"project_id": "123",
"worksite_id": "worksite_2"
},
{
"_id": "457",
"type": "document",
"code": "d002",
"project_id": "123",
"worksite_id": "worksite_2"
},
{
"_id": "458",
"type": "document",
"code": "d003",
"project_id": "123",
"worksite_id": "worksite_2"
},
{
"_id": "459",
"type": "document",
"code": "d001",
"project_id": "125",
"worksite_id": "worksite_1"
},
{
"_id": "789",
"type": "signature",
"user": "alice",
"pubkey": "65ab64c64ed64ef41a1bvc7d1b",
"code": "s001"
},
{
"_id": "790",
"type": "signature",
"user": "carol",
"pubkey": "tlmg90834kmn90845kjndf98734",
"code": "s002"
},
{
"_id": "791",
"type": "signature",
"user": "bob",
"pubkey": "asdf654asdf6854awer654awer654eqr654wra6354f",
"code": "s003"
},
{
"_id": "_design/projDocs",
"views": {
"docsPerWorkSite": {
"map": "function (doc) {\n if (doc.type && ['worksitelog', 'document', 'project', 'signature'].indexOf(doc.type) > -1) {\n if (doc.type == 'worksitelog') {\n emit([doc.worksite_id, 0], null);\n for (var i in doc.documents) {\n emit([doc.worksite_id, Number(i)+1, 'p'], {_id: doc.documents[i].project_id});\n emit([doc.worksite_id, Number(i)+1, 'd'], {_id: doc.documents[i].document_id});\n emit([doc.worksite_id, Number(i)+1, 's'], {_id: doc.documents[i].signature_id});\n }\n }\n }\n}"
}
},
"language": "javascript"
}
]
}
将该数据另存为stackoverflow_53752001.json
到磁盘。
使用Fauxton创建一个名为stackoverflow_53752001
的数据库。
这是一个bash
脚本,用于从文件stackoverflow_53752001.json into the database
stackoverflow_53752001`加载数据。显然,您需要编辑前三个参数。对其进行修复,然后将其粘贴到(Unix)终端窗口中:
USRID="you";
USRPWD="yourpwd";
HOST="yourdb.yourpublic.work";
COUCH_DATABASE="stackoverflow_53752001";
FILE="stackoverflow_53752001.json";
#
COUCH_URL="https://${USRID}:${USRPWD}@${HOST}";
FULL_URL="${COUCH_URL}/${COUCH_DATABASE}";
curl -H 'Content-type: application/json' -X POST "${FULL_URL}/_bulk_docs" -d @${FILE};
在Fauxton中,选择数据库stackoverflow_53752001
,然后在左侧菜单中选择“设计文档” >>“ projDocs” >>“视图” >>“ docsPerWorkSite”。
您将看到如下数据:
{"total_rows":17,"offset":0,"rows":[
{"id":"1235","key":["worksite_1",0],"value":null},
{"id":"1235","key":["worksite_1",1,"d"],"value":{"_id":"459"}},
: :
: :
{"id":"1234","key":["worksite_2",4,"p"],"value":{"_id":"123"}},
{"id":"1234","key":["worksite_2",4,"s"],"value":{"_id":"790"}}
]}
如果您随后单击右上角的“选项”按钮,则将获得用于修改原始查询的选项表。选择:
点击“运行查询”,您应该看到:
{"total_rows":17,"offset":0,"rows":[
{"id":"1235","key":["worksite_1",0],"value":null,"doc":{"_id":"1235","_rev":"1-de2b919591c70f643ce1005c18da1c54","type":"worksitelog","documents":[{"timestamp":"20180913101502","project_id":"125","document_id":"459","signature_id":"790"}],"worksite_id":"worksite_1"}},
{"id":"1235","key":["worksite_1",1,"d"],"value":{"_id":"459"},"doc":{"_id":"459","_rev":"1-5422628e475bab0c14e5722a1340f561","type":"document","code":"d001","project_id":"125","worksite_id":"worksite_1"}},
{"id":"1235","key":["worksite_1",1,"p"],"value":{"_id":"125"},"doc":{"_id":"125","_rev":"1-312dd8a9dd432168d8608b7cd9eb92cd","type":"project","code":"p003"}},
{"id":"1235","key":["worksite_1",1,"s"],"value":{"_id":"790"},"doc":{"_id":"790","_rev":"1-be018df4ecdf2e6add68a2758b9bd12a","type":"signature","user":"carol","pubkey":"tlmg90834kmn90845kjndf98734","code":"s002"}}
]}
如果随后将开始键和结束键更改为["worksite_2", 0]
和["worksite_2", 9999]
,则将看到第二个工作站点的数据。
为此,每次您向数据库中写入新文档和签名时,都需要:
{
"timestamp": "20180921091502",
"project_id": "123",
"document_id": "457",
"signature_id": "791"
}
documents
数组我假设每个文档有多个签名,所以您必须为每个文档编写一个日志记录。我认为,如果该值变得太大,则可以将worksite_id
更改为worksite_1_201812
之类,这样每个工作站点每个月可以提供一个日志,而不会破坏查询逻辑。