我正在寻找一个相当于"SQL“的CouchDB。
在我的示例中,有一些CouchDB文档是列表元素:
{ "type" : "el", "id" : "1", "content" : "first" }
{ "type" : "el", "id" : "2", "content" : "second" }
{ "type" : "el", "id" : "3", "content" : "third" } 有一个文件定义了列表:
{ "type" : "list", "elements" : ["2","1"] , "id" : "abc123" }正如您可以看到的,第三个元素被删除了,它不再是列表的一部分。因此,它绝不能成为结果的一部分。现在,我想要一个返回内容元素的视图,包括正确的顺序。
其结果可能是:
{ "content" : ["second", "first"] }在这种情况下,元素的顺序已经达到了应有的程度。另一个可能的结果是:
{ "content" : [{"content" : "first", "order" : 2},{"content" : "second", "order" : 1}] }我开始编写地图函数:
map = function (doc) {
if (doc.type === 'el') {
emit(doc.id, {"content" : doc.content}); //emit the id and the content
exit;
}
if (doc.type === 'list') {
for ( var i=0, l=doc.elements.length; i<l; ++i ){
emit(doc.elements[i], { "order" : i }); //emit the id and the order
}
}
}这是我所能得到的。你能纠正我的错误,写一个精简函数吗?请记住,第三个文档不能是结果的一部分。
当然,您也可以编写不同的映射函数。但是,不能更改文档的结构(每个条目有一个定义元素文档和一个条目文档)。
编辑:不要错过JasonSmith对他的回答的评论,在他的回答中,他描述了如何做得更短。
https://stackoverflow.com/questions/3033443
复制相似问题