1
votes

I am newbie to write couchdb map and reduce queries. One of my requirements is based on some keys we have to emit the data. i wrote Successfully for that as mentioned below.

function(doc) {
emit([doc.type , doc.category, doc.user], doc);

}

If we give three key values like

 http://localhost:5984/test/_design/myDesign/_view/myView?key=["Science","A","John"] 

then that data will be emitted. But my doubt is how can i get the data if we give one key or two keys for that view. That means for the above view if the user mentioned one key like doc.user then the user data will be emitted based on the three keys. And one more doubt is, Can we give two values for one key? ? Suppose the user wants to get the data either user name is JOHN or Joe by using the same view. Please provide guidance to achieve.

Thanks.

1

1 Answers

1
votes

You could change your view this way:

emit([doc.user, doc.type, doc.category], doc);

and then ask for

?startkey=["someName"]&endkey=["someName",{}] 

then in the resultlist are all documents that have doc.user="someName", regardless of type and category

if you need more specific informations, you can add another criteria to your call:

?startkey=["someName","someType"]&endkey=["someName","someType",{}] 

then you receive all docs with someName AND someType.

But you have to use the same order (key fields must be filled from left to right)

if you need another order (look for only types), you have to do a second emit with the other order in the same function (or in another function).

Asking for two keys at the same time is only possible with the hole key given. then you have to call a view py POST with body

{ keys=[["Science","A","John"],["Science","A","Joe"]] }