How can I name another view in couchdb view?

I just finished the book "couchdb: the ultimate guide" and started playing with project documents. There is one thing that I do not understand. All the examples I've seen so far are somewhat linear.

Example:

{
   "_id": "1",
   "_rev": ".....",
   "name": "first",
   "something": "blue",   
   "child": "2"   
}

{
   "_id": "2",
   "_rev": ".....",
   "name": "second",
   "something": "green",   
   "child": "3"   
   "parent" : "1"
   }

{
   "_id": "3",
   "_rev": ".....",
   "name": "second",
   "something": "red",   
   "parent" : "2";
}

I have no problem writing a view that returns all colors:

function(doc) {
        if (doc.something) {
            emit(doc.something,doc._id);    
    }
}

But what if I want to know all (!) Descendants ( not children, sorry for my mistake ) for an element with _id = 1 ("something": "blue")? My programming experience tells me that I should use recursion, but I don't know how to do this. How can I call another view function from the view function?

: , json-. , .

Edit: : _id = 1, - [_id = 2, _id = 3], 2 1 3 2.

+5
2

, - CouchDB .

. () ().

, .

( parent/child), CouchDB: , , ..

CouchDB , node :

{
   "_id": "1",
   "name": "first",
   "something": "blue",
   "path": [1]
}

{
   "_id": "2",
   "name": "second",
   "something": "green",
   "path": [1,2]
   }

{
   "_id": "3",
   "name": "second",
   "something": "red",
   "path": [1,2,3]
}

:

function(doc) { 
    for (var i in doc.path) { 
        emit([doc.path[i], doc.path], doc) 
    } 
}

_id 1, :

http://c.com/db/_design/colors/_view/descendants?startkey=[1]&endkey=[1,{}]

. CouchDB . Paul Bonser.

+8

, , :

function (doc) {
    if (doc.parent) {
        emit(doc.parent, { "_id": doc._id });
    }
}

( "", 2, .)

, :

[ "1", { "_id": "2" } ]
[ "2", { "_id": "3" } ]

, :

http://.../db/_design/viewName/_view/childfunc?key="2"

, include_docs .

, :

function (doc) {
    emit([ doc._id, "" ], { "_id": doc.id });
    if (doc.parent) {
        emit([ doc.parent, doc._id ], { "_id": doc.id })
    }
}

, :

[ [ "1", ""  ], { "_id": "1" } ]
[ [ "1", "2" ], { "_id": "2" } ]
[ [ "2", ""  ], { "_id": "2" } ]
[ [ "2", "3" ], { "_id": "3" } ]
[ [ "3", ""  ], { "_id": "3" } ]

( - ""), . _id , , . ( , , , .)

"child", , :

function (key, vals) {
    var children = [];
    for (var docId in vals) {
        if (key[1] !== "") {
            children.push(docId);
        }
    }
    return children;
}

, , . , .

+1

All Articles