user3861494
user3861494

Reputation: 11

couchdb get value of variable keys with view mapper

I am a new user of CouchDB and have tried to use the official documentation to answer my question, but have so far failed to find a method for performing what should be a simple operation.

I know that one can obtain the value of a known key, say "tacos", from a document "doc" via "doc.tacos". I need to access the value of a variable key in a view mapper. I am looking for a code solution that does something like

"get-key-value": {
   "map": "function(doc) { var key; \** get key id **\ emit(key,doc.getValue(key)); }"
}

I need to figure out how to perform the "doc.getValue(key)" part of this query. If I knew that the key variable was always going to be "tacos", then I could use "doc.tacos", but I'm dealing with a situation where I may want different keys depending on the situation. It seems like there should be a simple method for doing this, but I've not managed to find it.

Upvotes: 1

Views: 1239

Answers (2)

Ewan Makepeace
Ewan Makepeace

Reputation: 5416

You are thinking procedurally. CDB is designed with a different mindset - one that sacrifices view size for read performance. You emit ALL keys to a view and then at query time can find any key you want (warning pseudo code ahead):

function(doc){
  for (var fieldName in doc){
    emit(fieldName, doc[fieldName]);
  }
} 

Now when you access the view for given key (and optionally value) you will get all the docs carrying that key. You might have to filter them based on some external logic to find the subset of interest?

Upvotes: 1

Akshat Jiwan Sharma
Akshat Jiwan Sharma

Reputation: 16060

Unfortunately this is not possible. A view function can only access the values of a single doc. Is show function what you need? You can pass in a variable to show and access it via a req parameter.

Edit

Okay I see what you mean. I don't have anything to add that helps you find a solution but I have a couple of things to say. I hope it helps you.

When you create a couchdb view you create a querying mechanism for your data. This mechanism is very specific. For example:-

function(doc){
emit(doc.time,null);
} 

The map function above creates a view that indexes your data sorted by the key time. You know before hand that you will query this view by time value. This is the way all couchdb views work. So what I am trying to say is that even if you do manage to find a way to get value for your key how would you then query it?

Taking your example :

"get-key-value": { "map": "function(doc) { var key; ** get key id **\ emit(key,doc.getValue(key)); }" }

Suppose this key is = doc.name and you find a way to execute getValue() function. What use is this view to you? When you query it

/get-key-value?key=

What do you put in the key parameter?

Now I assume that your documents are flexible. One document may contain a field and another document may not have that field and you want to index only those documents that contain a certain field. The easiest way to do that is to use if statement.

"get-key-value": { "map": "function(doc) { var key; ** get key id **\ if(key) emit(key,doc.getValue(key)); }" }

This way get-key-value view will only emit when a specific key exists and thus index only those documents which contain that key.

Upvotes: 0

Related Questions