Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wildcards in Views with CouchDB?

Is it possible to use wildcards in views in CouchDB? For example, lets say I have a database that has teams, ages of players, players' averages, and gender of players. However, the players' ages may not be known - they could be from the Dominican Republic or whatnot. So I want to use a view with a map function that can accept not having the player's age. If I want to get the player with the highest average on a team regardless of age or with a specific age, how do I do so if I want to use compound keys? For example, lets say my map emit function is something like:

emit([doc.team, doc.gender, doc.age], doc.average);

What do I do if I want to pass in a key to narrow it down, yet still factor in the players who don't have an age entered, so instead of:

http://127.0.0.1:5984/savings/_design/players/_view/average?key=["Yankees","male",8]

doing something that incorporates a wildcard such as this to get the averages regardless of age:

http://127.0.0.1:5984/savings/_design/players/_view/average?key=["Yankees","male",*]

Is it possible to write a view in CouchDB to do something like this? Or do I need to write multiple views and process them separately?

Thanks,
Ben

like image 505
user21293 Avatar asked Sep 07 '10 01:09

user21293


2 Answers

You can specify startkey and endkey and use the "wildcard" {}, a JavaScript empty object:

http://127.0.0.1:5984/savings/_design/players/_view/average?startkey=["Yankees","male",0]&endkey=["Yankees","male",{}]
like image 185
noun Avatar answered Oct 16 '22 17:10

noun


Wildcard is not possible, but you can specify startkey and endkey:

http://127.0.0.1:5984/savings/_design/players/_view/average?startkey=["Yankees","male",0]&endkey=["Yankees","male",999]
like image 45
amorfis Avatar answered Oct 16 '22 15:10

amorfis