0
votes

I am attempting my first foray into writing to mongodb from erlang and came across suggestions to use the mongodb_erlang driver found here. However, I have created a very simple module similar to one of the tests from the repo:

-module(mongo_test).
-compile(export_all).


init() ->
    application:start(bson),
    application:start(mongodb).

insert_and_find() ->
    Host = {localhost, 27017},
    Conn = mongo_connection:start_link(Host, []),
    Database = baseball,
    Collection = teams,
    Teams = mongo:do(safe, master, Conn, Database, fun() ->
                                                           mongo:insert(Collection, [
                                                                                     {name, >, home, {city, >, state, >}, league, >},
                                                                                     {name, >, home, {city, >, state, >}, league, >},
                                                                                     {name, >, home, {city, >, state, >}, league, >},
                                                                                     {name, >, home, {city, >, state, >}, league, >}
                                                                                     ]),
    4 = mongo:count(Collection, []),
    Teams = find(Collection, {}),

    NationalTeams = [Team || Team >],
    NationalTeams = find(Collection, {league, >}),
    2 = mongo:count(Collection, {league, >}),

    TeamNames = [bson:include([name], Team) || Team >, state, >}})
    end).







%%private
find(Collection, Selector) ->
    find(Collection, Selector, []).
find(Collection, Selector, Projector) ->
    Cursor = mongo:find(Collection, Selector, Projector),
    Result = mongo_cursor:rest(Cursor),
    mongo_cursor:close(Cursor),
    Result.

However, after calling init() to start bson and mongodb, I then call insert_and_find and recieve the following error:

** exception exit: {{function_clause,[{gen,call,
                                           [{ok,},
                                            '$gen_call',
                                            {request,baseball,
                                                     {insert,teams,
                                                             [{name,>,home,
                                                                    {city,>,state,>},
                                                                    league,>,'_id',
                                                                    {>}}]}},
                                            infinity],
                                           [{file,"gen.erl"},{line,146}]},
                                      {gen_server,call,3,[{file,"gen_server.erl"},{line,184}]},
                                      {mongo_connection,request,3,
                                                        [{file,"src/mongo_connection.erl"},{line,48}]},
                                      {mongo,write,4,[{file,"src/mongo.erl"},{line,251}]},
                                      {mongo,write,1,[{file,"src/mongo.erl"},{line,234}]},
                                      {mongo,insert,2,[{file,"src/mongo.erl"},{line,78}]},
                                      {mongo,insert,2,[{file,"src/mongo.erl"},{line,75}]},
                                      {erl_eval,do_apply,6,[{file,"erl_eval.erl"},{line,573}]}]},
                    {gen_server,call,
                                [{ok,},
                                 {request,baseball,
                                          {insert,teams,
                                                  [{name,>,home,
                                                         {city,>,state,>},
                                                         league,>,'_id',
                                                         {>}}]}},
                                 infinity]}}
     in function  gen_server:call/3 (gen_server.erl, line 188)
     in call from mongo_connection:request/3 (src/mongo_connection.erl, line 48)
     in call from mongo:write/4 (src/mongo.erl, line 251)
     in call from mongo:write/1 (src/mongo.erl, line 234)
     in call from mongo:insert/2 (src/mongo.erl, line 78)
     in call from mongo:insert/2 (src/mongo.erl, line 75)

The error is fairly obscure to me and I have not been able to find any help online. Any suggestions would be greatly appreciated.

Best.

1

1 Answers

1
votes

Try to replace:Conn = mongo_connection:start_link(Host, []), with {ok, Conn} = mongo_connection:start_link(Host, []),