combine multiple documents in a couchdb view

断了今生、忘了曾经 提交于 2020-01-11 03:13:46

问题


In couchdb, I need to represent some data in the following format, a outer container that references other documents inside an array.

I want to keep these documents separate as I need to manage conflicts on them individually.

{
  "_id" : "1"'
  "type" : "container",
  "items" : [ "1", "2", "3"]
}


{
   "_id" : "2",
   "value": "a"
    "type" : "item"
}


{
   "_id" : "3",
   "value": "b"
   "type" : "item"
 }


 {
   "_id" : "4",
   "value": "c"
    "type" : "item"
 }    

I want to output a view of the data in the following format.

{
  "_id" : "1"'
  "type" : "container",
  "items" : [
     {
       "_id" : "2",
       "value": "a"
       "type" : "item"
     },
     {
       "_id" : "3",
       "value": "b"
       "type" : "item"
     },
     {
       "_id" : "4",
       "value": "c"
       "type" : "item"
     }
   ]

}

Whats the best way to approach this?


回答1:


You can achieve this using couchdb linked documents

here is how the view will look like

  function(doc){
    if(doc.items)
    doc.items.forEach(function(item){
          emit(doc._id,{_id:item});
      })
 }

now you can query the view with include_docs=true parameter and you should have the desired result.



来源:https://stackoverflow.com/questions/24264898/combine-multiple-documents-in-a-couchdb-view

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