CouchDB multi level view map function?

岁酱吖の 提交于 2020-01-04 06:57:08

问题


I'm not quite sure what to call what I'm trying to achieve, but will explain it as much as possible. I am trying to use a view to return information from separate documents that are related like a hierarchy. For example, I have these three documents.

{
  "_id" : "line_2c7e12d1",
  "docType" : "Line",
  "lineNumber": 30,
  "pageId" : "page_89bdd679f"
}

{
  "_id" : "page_89bdd679f",
  "docType" : "Page",
  "pageNumber" : 65,
  "bookId" : "book_3684caa2b"
}

{
  "_id" : "book_3684caa2b",
  "docType" : "Book",
  "bookName" : "Some Book Title",
}

And I would like to be able to create a view that allows finding information from about the Book based on the Line id.(multi level hierarchy).

For example, something like this for the View Map function.

function (doc) {
  if(doc.docType == 'Line'){
    emit([doc._id, 1], doc.lineNumber)
    emit([doc._id, 2], {_id : doc.pageId})
    emit([doc._id, 3], {_id : doc.pageId}.pageNumber)
    emit([doc._id, 4], {_id : {_id : doc.pageId}.bookId})
  }
}

I know the first two emit lines work correctly, but the second two do not. I am just wondering if this kind of functionality is possible within CouchDB or if I should structure my data differently. (Include all hierarchy information at the lowest level so it can be searched upwards). Or if anyone has any other suggestions.

Thanks Callum


回答1:


It seems that you are trying to include in the view information from related documents. In the map function you only have access to the current doc and you can not query the database for any other document.

You can obtain a kind of "join" functionality following this approach see.

Your data structure is keeping a strong relational flavour that could not be the best approach for CouchDB which is a document oriented database.



来源:https://stackoverflow.com/questions/54283431/couchdb-multi-level-view-map-function

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