[erlang-questions] TonyGen mongodb-erlang / incrementing a field

Daniel Luna <>
Sun Sep 4 19:10:43 CEST 2011


On 2 September 2011 15:30, Slav Pankratov <> wrote:
> I'm struggling to increment a field ($inc) in mongodb from erlang via the
> official mongo driver (https://github.com/TonyGen/mongodb-erlang).
> Could anyone point me in the right direction of one of the correct ways to
> do it?

You can do something like the following:

F = fun() -> mongo:modify(some_collection, {some_indexed_field, Key},
{'$inc', {'field.to.increase', Increase}}) end,
mongo:do(safe, master, Connection, db_name, F).

If the field to increase doesn't exist it will be created with value Increase.

Take a look at mongo:repsert/3 if the element might be non-existent.

Cheers,

Daniel



More information about the erlang-questions mailing list