使用erlang mongodb驱动程序进行数据库操作的正确方法是什么? [英] What's the right way to use erlang mongodb driver to make a db action?
问题描述
每次我采取行动,我只是写下如下内容:
{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()。
-
这是一个正确的方法吗?每当我完成数据库操作,
Conn
似乎死了。或者我应该保持Conn
而不是断开它,以便下次重用?没有全局变量可以保持Conn
,所以我想出来的唯一方法是使用类似gen_server
并在其州保留Conn
以供重用。这是正确的方法吗? -
我还看到有一个
connect_factory
方法。但是我不能弄清楚处理它的正确方法。一个比连接处理大量db操作更好的方式是connect_factory
?如何通过使用connect_factory
?
c> Conn li>
这是一个与mongodb不相关的问题。我想给每个用户一个唯一的号码,当他们访问。所以我在数据库中保存了一个计数器,当用户访问时,计数器被加1并返回给用户作为唯一的数字。但是我一直都关心两位用户在同一时间读取数据库的数量。使用mongodb如何使独特的计数器增加1?
非常感谢!
- 如果您的操作有错误,Conn可能会死机,否则应该是可重用的。注意,mongo:do返回{failure,X}错误。如果要重复使用,请勿断开连接。 BTW,断开连接将Conn作为参数。如果要将Conn保留在全局变量中,请参考Erlang文档以了解如何执行此操作(例如,ets表,gen_server等)。此外,请查看此驱动程序中的mvar模块,以获取一个包含值的简单gen_server。
- connect_factory用于维护可重用的连接池。请阅读 http://github.com/TonyGen/mongodb-erlang 上的教程。在底部显示如何使用connect_factory与resource_pool。
- 您可以使用MongoDB进行原子增量。请参阅 http://www.mongodb.org/display/DOCS/Atomic+Operations,特别是findAndModify。
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!
- Conn may die if your action had an error, otherwise it should be reusable. Note, mongo:do returns {failure, X} on error. Don't disconnect Conn if you want to reuse it. BTW, disconnect takes Conn as an argument. If you want to keep Conn in a global var then consult the Erlang documentation for ways to do this (eg. ets table, gen_server, etc). Also, check out the mvar module in this driver for a simple gen_server that holds a value.
- connect_factory is used for maintaining a reusable pool of connections. Read the tutorial at http://github.com/TonyGen/mongodb-erlang. At the bottom it shows how to use connect_factory with resource_pool.
- You can atomically increment using MongoDB. See http://www.mongodb.org/display/DOCS/Atomic+Operations, in particular findAndModify.
这篇关于使用erlang mongodb驱动程序进行数据库操作的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!