【发布时间】:2015-02-10 06:01:10
【问题描述】:
我有一个 Erlang Couch DB,它呈现一个 JSON 对象,如下所示:
fun({Doc}) ->
Name = couch_util:get_value(<<\"name\">>, Doc),
Value = couch_util:get_value(<<\"Value\">>, Doc),
Geocode = couch_util:get_value(<<\"geocode\">>, Doc),
Emit(
Name,
{[
{ <<\"value\">>,Value },
{ <<\"geocode\">>, Geocode }
]}
)
end.
问题在于此视图中的所有文档都没有“地理编码”属性。在不存在地理编码的情况下,我宁愿不显示它。在伪代码中,我基本上想这样做......
Emit(
Name,
{[
{ <<\"value\">>,Value },
Geocode != undefined ? { <<\"geocode\">>, Geocode } : null
]}
);
我怀疑在 Erlang 中做到这一点不会那么容易?
到目前为止,我最好的解决方案是:
fun({Doc}) ->
Name = couch_util:get_value(<<\"name\">>, Doc),
Value = couch_util:get_value(<<\"Value\">>, Doc),
Geocode = couch_util:get_value(<<\"geocode\">>, Doc),
% I think 'couch_util:get_value' returns the atom undefined, if the value doesn't exist.
Undefined = undefined,
if Geocode /= Undefined ->
Emit(
Name,
{[
{ <<\"value\">>, Value },
{ <<\"geocode\">>, Geocode }
]}
);
true -> Emit(Name, {[ { <<\"value\">>, Value } ]})
end;
end.
很确定那里会有一两个语法错误...请随时指出它们! 但更重要的是,有没有更有效的方法来有条件地从 proplist / 视图中删除 'geocode' 值?
【问题讨论】:
标签: syntax mapreduce erlang couchdb conditional