使用erlang mongodb驱动程序进行数据库操作的正确方法是什么? [英] What's the right way to use erlang mongodb driver to make a db action?

查看:126
本文介绍了使用erlang mongodb驱动程序进行数据库操作的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试官方的mongodb erlang司机。我阅读了文档,还有一些我不明白的东西。希望有人可以告诉我使用它的正确方法:



每次我采取行动,我只是写下如下内容:

  {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()。




  1. 这是一个正确的方法吗?每当我完成数据库操作, Conn 似乎死了。或者我应该保持 Conn 而不是断开它,以便下次重用?没有全局变量可以保持 Conn ,所以我想出来的唯一方法是使用类似 gen_server 并在其州保留 Conn 以供重用。这是正确的方法吗?


  2. 我还看到有一个 connect_factory 方法。但是我不能弄清楚处理它的正确方法。一个比连接处理大量db操作更好的方式是 connect_factory ?如何通过使用 connect_factory


  3. c> Conn li>

    这是一个与mongodb不相关的问题。我想给每个用户一个唯一的号码,当他们访问。所以我在数据库中保存了一个计数器,当用户访问时,计数器被加1并返回给用户作为唯一的数字。但是我一直都关心两位用户在同一时间读取数据库的数量。使用mongodb如何使独特的计数器增加1?


非常感谢!

解决方案


  1. 如果您的操作有错误,Conn可能会死机,否则应该是可重用的。注意,mongo:do返回{failure,X}错误。如果要重复使用,请勿断开连接。 BTW,断开连接将Conn作为参数。如果要将Conn保留在全局变量中,请参考Erlang文档以了解如何执行此操作(例如,ets表,gen_server等)。此外,请查看此驱动程序中的mvar模块,以获取一个包含值的简单gen_server。

  2. connect_factory用于维护可重用的连接池。请阅读 http://github.com/TonyGen/mongodb-erlang 上的教程。在底部显示如何使用connect_factory与resource_pool。

  3. 您可以使用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().

  1. Is this a right way? Everytime I finished a db action, Conn seems died. Or should I keep the Conn rather than disconnect it to reuse it for next time? There's no global variable to keep Conn so the only way I can think out to make it is to use something like gen_server and keep Conn in its State for reuse. Is this the right way?

  2. I also see there's a connect_factory method. But I can't quite figure out the proper way to deal with it. Is connect_factory a better way than connect to deal with large amount of db actions? And how to get a workable Conn by using connect_factory?

  3. 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!

解决方案

  1. 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.
  2. 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.
  3. You can atomically increment using MongoDB. See http://www.mongodb.org/display/DOCS/Atomic+Operations, in particular findAndModify.

这篇关于使用erlang mongodb驱动程序进行数据库操作的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆