在 CouchDB 中从 Erlang 视图中发出元组 [英] Emit Tuples From Erlang Views In CouchDB
问题描述
CouchDB,版本 0.10.0,使用原生 erlang 视图.
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"
}
还有一份基本的 javascript 设计文档:
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});
});
}
}"
}
}
}
发出:
{"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"}}
]}
我正在研究建立一个做同样事情的 Erlang 视图.迄今为止最好的尝试是:
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.
发出:
{"total_rows":3,"offset":0,"rows":[
{"id":"user-1","key":"ABC","value":["monkey","Monkey Man"]},
{"id":"user-1","key":"DEF","value":["monkey","Monkey Man"]},
{"id":"user-1","key":"123","value":["monkey","Monkey Man"]}
]}
问题是 - 我怎样才能将这些值作为元组而不是数组取出?我不认为我可以(或不想)使用记录,但在元组中使用原子似乎不起作用.
The question is - how can I get those values out as tuples, instead of as arrays? I don't imagine I can (or would want to) use records, but using atoms in a tuple doesn't seem to work.
lists:foreach(fun(Identifier) -> Emit(Identifier, {id, ID, username, Username, name, Name}) end, Identifiers);
失败并出现以下错误:
{"error":"json_encode","reason":"{bad_term,{<<"user-1">>,<<"monkey">>,<<"Monkey Man">>}}"}
想法?我知道 Erlang 不适合这种特定类型的事情(命名访问),而且我可以按照约定来做(id 在第一个位置,用户名在下,真名在后),但这使得客户端代码非常难看.
Thoughts? I know that Erlang sucks for this specific kind of thing (named access) and that I can do it by convention (id at first position, username next, real name last), but that makes the client side code pretty ugly.
推荐答案
JSON 对象 {"foo":"bar","baz":1}
是 {[{<<"foo">>,<<"bar">>},{<<"baz">>,1}]}
The JSON object {"foo":"bar","baz":1}
is {[{<<"foo">>,<<"bar">>},{<<"baz">>,1}]}
在 Erlang lingua 中,它是一个包裹在元组中的 proplist.
In Erlang lingua it is a proplist wrapped in a tuple.
它不漂亮,但非常有效:)
It's not pretty, but very efficient :)
为了感受它,您可以使用 CouchDB 附带的 JSON 库:
To get a feel for it you can play with the JSON lib that ships with CouchDB:
- 使用 -i 启动 CouchDB(交互式)标志
- 在生成的 erlang shell 中,输入:
couch_util:json_decode(<<"{"foo":"bar"}">>).
- 利润
//在 CouchDB 的更高版本中,这是 ejson:decode()
// in later versions of CouchDB, this is ejson:decode()
这篇关于在 CouchDB 中从 Erlang 视图中发出元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!