Skip to content

Instantly share code, notes, and snippets.

@krakatoa
Last active December 26, 2016 13:22
Show Gist options
  • Save krakatoa/528b9198efcf237937d8 to your computer and use it in GitHub Desktop.
Save krakatoa/528b9198efcf237937d8 to your computer and use it in GitHub Desktop.
Erl Mongo snippets
find_probe:
===========
handle_call({find_probe, ProbeId}, _From, State) ->
{_Sup, {Mongo, _Redis}} = State,
ProbeOid = binary_string_to_objectid(ProbeId),
{R} = mongo:find_one(Mongo, <<"nodes">>, {'probes._id', ProbeOid}),
[Res|_] = lists:filter(fun(V) -> bson:at('_id', V) == ProbeOid end, bson:at(probes, R)),
{reply, Res, State};
binary_string_to_objectid(BinaryString) ->
binary_string_to_objectid(BinaryString, []).
binary_string_to_objectid(<<>>, Result) ->
{list_to_binary(lists:reverse(Result))};
binary_string_to_objectid(<<BS:2/binary, Bin/binary>>, Result) ->
binary_string_to_objectid(Bin, [erlang:binary_to_integer(BS, 16)|Result]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment