Skip to content

Instantly share code, notes, and snippets.

@ngerakines
Created October 19, 2009 21:12
Show Gist options
  • Select an option

  • Save ngerakines/213710 to your computer and use it in GitHub Desktop.

Select an option

Save ngerakines/213710 to your computer and use it in GitHub Desktop.
-module(emonog_tools).
-export([replace/3, replace_add/4]).
replace(Collection, Find, Items) ->
#response{documents = [Doc]} = emongo:find(iplaywar, Collection, Find),
NewDoc = lists:foldl( fun replace_add/2, Doc, Items ),
emongo:update(iplaywar, Collection, Find, NewDoc, true).
replace_add({Name, Value}, Doc) ->
case lists:keymember(Name, 1, Doc) of
true -> lists:keyreplace(Name, 1, Doc, {Name, Value});
false -> [{Name, Value} | Doc]
end;
replace_add({delete, Name, Value}, Doc) ->
lists:keydelete(Name, 1, Doc);
replace_add({asort, Name, Value}, Doc) ->
OldValue = case proplists:get_value(Name, Doc, []) of <<>> -> []; X -> X end,
NewValue = lists:usort(Value ++ OldValue),
lists:keyreplace(Name, 1, Doc, {Name, NewValue});
replace_add({zsort, Name, Value}, Doc) ->
OldValue = case proplists:get_value(Name, Doc, []) of <<>> -> []; X -> X end,
NewValue = lists:delete(Value, OldValue),
lists:keyreplace(Name, 1, Doc, {Name, NewValue}).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment