I use mongodb-erlang driver for mongo db access in erlang. Some of my command execution:
34> {ok, Conn} = mongo:connect({localhost, 27017}).
{ok,{connection,{"localhost",27017},
<0.89.0>,false,infinity}}
35> {ok, Data} = mongo:do(safe, master, Conn, homeweb, fun() -> mongo:find_one(user, {apartmentId, 1}) end).
{ok,{{'_id',{<<79,180,252,18,220,119,245,66,215,79,71,61>>},
apartmentId,1.0,email,<<"e@mail.com">>,password,
<<"efe6398127928f1b2e9ef3207fb82663">>}}}
Data is a tuple.
For example in php array is returned from find request and I can get id with code like this: $id = $result['_id'];.
The question is: how to access to fetched from db data in Erlang?
By pattern matching. In this case, “Data” holds the result, so you might do something like:
The words that start with an upper case will hold the values, so for example, you can print them:
EDIT: You are actually doing pattern matching when you run your query. Notice the {ok, Data} = on the left side of the = operator. This is effectively matching that the result is a tuple in the form {ok, Data} and since Data is unbound up to that point, it is assigned to the query result.
EDIT2: Since Data in this case is a bson(), you can refer to the erlang bson module (used as a dependency of the mongodb erlang driver): http://api.mongodb.org/erlang/bson/. There are specific functions you can use in this case, like bson:lookup/2 and bson:fields/1, passing as a parameter the bson() document (the result from mongodb:find_one/2)