Last active
December 26, 2016 13:22
-
-
Save krakatoa/528b9198efcf237937d8 to your computer and use it in GitHub Desktop.
Erl Mongo snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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