incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jan Lehnardt <>
Subject Re: efficiency of temporary views
Date Wed, 30 Apr 2008 13:11:59 GMT
On Apr 30, 2008, at 14:16, Jonathan Moss wrote:
> Greetings all,
> I have been lurking for a couple of weeks now and am about to embark  
> on
> little project using couch DB and have a couple of questions.
> I am trying to come up with a flexible way to model an object  
> hierarchy.
> My current thoughts run something like
> {
>    _id: xxx
>    _rev: yyy
>    type: something
>    n.e.other: foo
>    parents: ["xxx","xxx"]
>    children: ["yyy","yyy"]
> }
> so a simple view (pseudo-code) to get all the children of an object
> (with id = 123) would be:
> function(doc):
>    if(doc.parents contains "123"){
>       map(doc._id,doc);
>    }
> }
> Obviously this kind of view cannot be persisted as the value if id  
> would
> need to change for every document in the DB.
> Would this be terribly in-efficient as it would have to be a temporary
> view or am I missing a trick?
> Could I do something cunning with the key
> field in the map function and the start/end_key get params?

Yeah :)

function(doc) {
   for(var idx in doc.parents) {
     map(doc.parents[idx], doc);

and then you can use the startkey parameters to get all docs
that have the parent "123". The Same for children and and
anything else you need.

> This also extends to the question of how to deal with running
> getChildren and only returning those of a specific 'type' e.g.
> if(doc.type = 'atype' && doc.parents contains "123"){
>    ...
> }

map([doc.parent[idx], doc.type], doc);

You can have complex JSON structures as the key that
allows you to collate by more than one attribute.

Don't be shy of adding more views :)

Beware though, at the moment you store the full doc
in the view, effectively doubling data. If that is okay
for you application and amount of data, never mind.
If you want to be a tad more conservative, store NULL
as the map value and retrieve only the document ids
from the view and the document data with subsequent
requests. Trade-offs and all that.


View raw message