CouchDB通过文档字段内部联接? [英] CouchDB inner join by document field?
问题描述
我有一个问题,我有2种文档,其中一个像这样:
I have a question, i have 2 kind of documents, one of them is like this:
{
"type": "PageType",
"filename": "demo"
"content": "zzz"
}
和另一个类似的东西:
{
"type": "PageCommentType",
"refFilename": "demo"
"content": "some comment content"
}
我需要发出包含.comments字段的文档,该字段是我按条件PageType document filename == PageCommentType document refFilename字段链接的PageCommentType文档的数组。
i need to emit document that contains .comments field which is array of PageCommentType documents that i link on condition PageType document filename == PageCommentType document refFilename fields.
{
"filename": "demo",
"comments": [{}, {}, {}]
}
有人对如何实施有何建议?
Anyone has any suggestions on how to implement it?
谢谢。
推荐答案
您需要查看排序规则 。使用文件名
作为键和类型标识符在同一个视图中发射两者,以区分注释和原始内容:
You need view collation. Emit both within the same view, using the filename
as the key and a type identifier to discriminate between comments and the original content:
function(doc) {
if (doc.type == "PageType") emit([doc.filename,0],doc.content);
if (doc.type == "PageCommentType") emit[doc.refFilename,1],doc.content);
}
在查找文档 demo $ c时$ c>及其注释,使用
startkey = [ demo,0]
和 endkey = [ demo,1] <运行查询/ code>:您将获得页面内容以及所有注释。
When looking for the document demo
and its comments, run a query with startkey=["demo",0]
and endkey=["demo",1]
: you will get the page content followed by all the comments.
一旦拥有了所需的所有数据,但是它的格式格式不正确,您快完成了。只需编写 _list
函数即可读取所有行,并输出具有所需结构/架构的最终JSON文档。
Once you have all the data you want, but it's not in the right format, you are almost done. Simply write a _list
function to read all the rows and output the final JSON document with the structure/schema that you need.
这篇关于CouchDB通过文档字段内部联接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!