Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

combine multiple documents in a couchdb view

Tags:

couchdb

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?

like image 551
Alan Quigley Avatar asked Jan 11 '23 09:01

Alan Quigley


1 Answers

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.

like image 182
Akshat Jiwan Sharma Avatar answered Feb 07 '23 21:02

Akshat Jiwan Sharma