CouchDB inner join by document field?

那年仲夏 提交于 2019-12-12 12:27:55

问题


I have a question, i have 2 kind of documents, one of them is like this:

{
  "type": "PageType",
  "filename": "demo"
  "content": "zzz"
}

and another one like this:

{
  "type": "PageCommentType",
  "refFilename": "demo"
  "content": "some comment content"
}

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?

Thank you.


回答1:


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);
}

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.

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.



来源:https://stackoverflow.com/questions/4787711/couchdb-inner-join-by-document-field

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!