_id sequence not matching
AbhishekLYK opened this issue · 12 comments
After reading library I found you are generating oid by your own. But as I have own db from with huge data, newly generated _id by this library is smaller than that I have. It's pattern not matching with my own db _id which is generated by mongodb itself. Any suggesstion?
And I have to work with sorting with that id.
It only creates it if it does not exist. So set _id yourself
If exists then also creating wrong pattern by default. My collection ids are like
{ "_id" : "58f5aa4c5b1c503545d24b3b" }
{ "_id" : "58f5aa715b1c503545d24b3c" }
{ "_id" : "58f5aab65b1c503545d24b3e" }
{ "_id" : "58f5aaea5b1c503545d24b3f" }
{ "_id" : "58f5c0d05b1c503545d24b47" }
{ "_id" : "58f5e7145b1c503545d24b4f" }
{ "_id" : "58f5e8675b1c503545d24b53" }
{ "_id" : "58f5e8a75b1c503545d24b55" }
{ "_id" : "58f5ecd4310a90370aefff7f" }
{ "_id" : "58f5ed7f310a90370aefff80" }
{ "_id" : "58f5eec9310a90370aefff83" }
{ "_id" : "58f5ef4d310a90370aefff84" }
{ "_id" : "58f5f01d310a90370aefff88" }
{ "_id" : "58f5f037310a90370aefff89" }
{ "_id" : "58f5f041310a90370aefff8a" }
{ "_id" : "58f5f110310a90370aefff8d" }
{ "_id" : "58f5f158310a90370aefff8e" }
{ "_id" : "58f5f4b3310a90370aefff8f" }
{ "_id" : "58f5f4fb310a90370aefff90" }
{ "_id" : "58f5f51c310a90370aefff91" }
and when I save by mongoapi:save("test", #{time=><<"1567663014000">>}, Mong).
It generates "003405e55926483322000002"
when generating with mongo console with same command db.test.insert({time:"1567663014000"}) It is giving right format { "_id" : ObjectId("58f5f51c310a90370aefff91"), "time" : "1567663014000" }
And where from I am going to get that _id?
New _id calculation in master
Not up to the mark yet, generating different kind of pattern
Where my pattern is
"5d789323b2c9c7dc5656b900"
"5d789391b2c9c7dc5656b901"
But new calculation gives
"c843856a4611a66653c53cc6"
"c8439ab48d567d1892c53cc7"
"c845177dec057bf6dbc53cc8"
Other library https://github.com/comtihon/mongodb-erlang is generating perfectly.
So use that
No, Your library's repicaset with mongo works much better and easier. I like it all. I have taken their bson and pached into my project. They are giving binary and I have converted that to hex. Erlmongo is easier to work with and understand. I will use this. Thanks for your concern.
-module(mongo_id_server).
-behaviour(gen_server).
-export([
request_id/0,
object_id/0
]).
-export([
start_link/0
]).
-export([
init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
terminate/2,
code_change/3
]).
-define(MAX_INT32, 2147483647).
%% @doc Fresh request id
-spec request_id() -> pos_integer().
request_id() ->
ets:update_counter(mongoid, requestid_counter, {2, 1, ?MAX_INT32, 0}).
%% @doc Fresh object id
-spec object_id() -> lykbson:objectid().
object_id() ->
Now = lykbson:unixtime_to_secs(lykbson:timenow()),
MPid = ets:lookup_element(mongoid, oid_machineprocid, 2),
N = ets:update_counter(mongoid, oid_counter, 1),
lykbson:objectid(Now, MPid, N).
-spec start_link() -> {ok, pid()}.
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
%% @hidden
init([]) ->
mongoid = ets:new(mongoid, [named_table, public, {write_concurrency, true}, {read_concurrency, true}]),
ets:insert(mongoid, [
{oid_counter, 0},
{oid_machineprocid, oid_machineprocid()},
{requestid_counter, 0}
]),
{ok, undefined}.
handle_call(_, _From, State) ->
{stop, 'bad-request', State}.
%% @hidden
handle_cast(_, State) ->
{noreply, State}.
%% @hidden
handle_info(_, State) ->
{noreply, State}.
%% @hidden
terminate(_, _State) ->
ok.
%% @hidden
code_change(_Old, State, _Extra) ->
{ok, State}.
%% @Private
-spec oid_machineprocid() -> <<_:40>>.
oid_machineprocid() ->
OSPid = list_to_integer(os:getpid()),
{ok, Hostname} = inet:gethostname(),
<<MachineId:3/binary, _/binary>> = erlang:md5(Hostname),
<<MachineId:3/binary, OSPid:16/big>>.
Above is working fine.