KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
CouchDB, version 0.10.0, using native erlang views. I have a simple document of the form: { "_id": "user-1", "_rev": "1-9ccf63b66b62d15d75daa211c5a7fb0d", "type": "user", "identifiers": [ "ABC", "DEF", "123" ], "username": "monkey", "name": "Monkey Man" } And a basic javascript design document: { "_id": "_design/user", "_rev": "1-94bd8a0dbce5e2efd699d17acea1db0b", "language": "javascript", "views": { "find_by_identifier": { "map": "function(doc) { if (doc.type == 'user') { doc.identifiers.forEach(function(identifier) { emit(identifier, {\"username\":doc.username,\"name\":doc.name}); }); } }" } } } which emits: {"total_rows":3,"offset":0,"rows":[ {"id":"user-1","key":"ABC","value":{"username":"monkey","name":"Monkey Man"}}, {"id":"user-1","key":"DEF","value":{"username":"monkey","name":"Monkey Man"}}, {"id":"user-1","key":"123","value":{"username":"monkey","name":"Monkey Man"}} ]} I'm looking into building an Erlang view that does the same thing. Best attempt so far is: %% Map Function fun({Doc}) -> case proplists:get_value(<<"type">>, Doc) of undefined -> ok; Type -> Identifiers = proplists:get_value(<<"identifiers">>, Doc), ID = proplists:get_value(<<"_id">>, Doc), Username = proplists:get_value(<<"username">>, Doc), Name = proplists:get_value(<<"name">>, Doc), lists:foreach(fun(Identifier) -> Emit(Identifier, [ID, Username, Name]) end, Identifiers); _ -> ok end end. which emits: {"total_rows":3,"offset":0,"rows":[ {"id":"user-1","key":"ABC","value":["monkey","Monkey Man"]}, {"id":"user-1","key":"DEF","value":["monkey",
Tags (comma-separated)
Save Edits
Cancel