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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With