本文介绍了在沙发数据库视图中合并多个文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在ouchdb中,我需要以以下格式表示一些数据,即一个外部容器,该外部容器引用数组中的其他文档。
In couchdb, I need to represent some data in the following format, a outer container that references other documents inside an array.
我想将这些文档分开
{ "_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?
推荐答案您可以使用 couchdb链接的文件
视图的外观如下
function(doc){ if(doc.items) doc.items.forEach(function(item){ emit(doc._id,{_id:item}); }) }现在您可以使用 include_docs = true 参数,您应该会得到所需的结果。
now you can query the view with include_docs=true parameter and you should have the desired result.
更多推荐
在沙发数据库视图中合并多个文档
发布评论