I want to use mongodb as a map in Erlang. I cannot find an example of this. This is what I have so far:
application:start(mongodb).
Host = {localhost, 27017}.
{ok, Conn} = mongo:connect(Host).
mongo:do(safe, master, Conn, test, fun() ->
mongo:delete(foo, {}),
mongo:insert(foo, {x,1}),
mongo:insert(foo, {a,12}),
Pid = mongo:find(foo, {a}),
Result = mongo_cursor:rest(Pid),
mongo_cursor:close(Pid),
Result end).
Result:
{ok,[{'_id',{<<80,138,211,178,41,152,132,104,251,0,0,35>>},
x,1},
{'_id',{<<80,138,211,178,41,152,132,104,251,0,0,36>>},
a,12}]}
Why does it create two maps?
You are doing two inserts into MongoDB without specifying any
_idvalue, so end up with two documents and an autogenerated unique ObjectId. If you are trying to use a collection similar to amapordict, you should be providing an_idvalue as the key and userepsert(aka upsert) to update the document if it exists already (or insert if it doesn’t).