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

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

问题描述

我正在尝试官方的 mongodb erlang 驱动程序.我阅读了文档,但仍有一些我不明白的地方.希望任何人都可以告诉我使用它的正确方法是什么:

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. 这是正确的方法吗?每次我完成一个 db 动作时,Conn 似乎都死了.或者我应该保留 Conn 而不是断开它以便下次重新使用它?没有保留 Conn 的全局变量,所以我能想到的唯一方法是使用类似 gen_server 的东西并将 Conn 保留在它的重用状态.这是正确的方法吗?

  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?

我还看到有一个 connect_factory 方法.但我无法弄清楚处理它的正确方法.connect_factory 是否比 connect 更好地处理大量数据库操作?以及如何使用 connect_factory 获得可行的 Conn?

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?

这是一个与 mongodb 不太相关的问题.我想在每个用户访问时给他们一个唯一的号码.所以我在db中保存了一个计数器,当用户访问时,计数器加1并作为唯一编号返回给用户.但是我总是担心两个用户同时读取 db 会得到相同的数字.如何使用 mongodb 使唯一计数器增加 1?

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?

非常感谢!

推荐答案

  1. 如果你的动作有错误,Conn 可能会死,否则它应该是可重用的.请注意,mongo:do 在出错时返回 {failure, X}.如果要重用它,请不要断开 Conn.顺便说一句,断开连接以 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.
  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天全站免登陆