I am trying official mongodb erlang driver. I read the doc and there's still something I can't understand. Hope anyone can tell me what's the right way to use it:
Every time I make an action, I just write something as follows:
{ok, Conn} = mongo:connect ({localhost, 27017}).
mongo:do (safe, master, Conn, test, fun() ->
mongo:save (foo, {'_id', 1, bbb,22, y,2}),
mongo:save (foo, {'_id', 4, bbb,22, y,2}) end).
mongo:disconnect().
Is this a right way? Everytime I finished a db action,
Conn
seems died. Or should I keep theConn
rather than disconnect it to reuse it for next time? There's no global variable to keepConn
so the only way I can think out to make it is to use something likegen_server
and keepConn
in its State for reuse. Is this the right way?I also see there's a
connect_factory
method. But I can't quite figure out the proper way to deal with it. Isconnect_factory
a better way than connect to deal with large amount of db actions? And how to get a workableConn
by usingconnect_factory
?This is a question not quite related with mongodb. I want to give every user a unique number when they visit. So I saved a counter in db, when a user visit, the counter is added by 1 and returned to the user as the unique number. But I always have concern about two users get the same number with reading db at the same time. How can I make a unique counter increased by 1 using mongodb?
Thanks very much!