How to convert objectid to binary subtype 3 (uuid) with mongodb-erlang and bson-erlang?

I generate object identifiers as follows: mongodb_app:gen_objectid() This returns an ObjectId type.

I need to have BinType (3, ...), since we are not storing object objects, but binary subtypes of 3 ids.

Does anyone know how to do this?

+3
source share
2 answers

figured out how to fix this, I'm using the avtobiff uuid generator to generate the UUID:

generate_objectid_subtype3() ->
    {bin, uuid, uuid:uuid4()}.
0
source
%%This method is used to generate ObjectId from binary string.
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]).

%%This method is used to generate binary string from objectid.
objectid_to_binary_string({Id}) ->
    objectid_to_binary_string(Id, []).

objectid_to_binary_string(<<>>, Result) ->
    list_to_binary(lists:reverse(Result));
objectid_to_binary_string(<<Hex:8, Bin/binary>>, Result) ->
    StringList1 = erlang:integer_to_list(Hex, 16),
    StringList2 = case erlang:length(StringList1) of
        1 ->
            ["0"|StringList1];
        _ ->
            StringList1
    end,
    objectid_to_binary_string(Bin, [StringList2|Result]).

:

  binary_string_to_objectid (< < "51F5BE99118735B187000001" → )
. Out Put:
  {& ; < 81,245,190,153,17,135,53,177,135,0,0,1 → }

  objectid_to_binary_string ({& ; & ; 81,245,190,153,17,135,53,177,135,0,0,1 → }).
Out Put:
& ; < "51F5BE99118735B187000001" →

+3

All Articles