我正在尝试第一次尝试从erlang写入mongodb并遇到使用mongodb_erlang驱动程序here的建议。但是,我创建了一个非常简单的模块,类似于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.
但是,在调用init()启动bson和mongodb后,我再调用insert_and_find并收到以下错误:
** 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)
错误对我来说相当模糊,我无法在网上找到任何帮助。任何建议将不胜感激。
最佳。
答案 0 :(得分:1)
尝试将Conn = mongo_connection:start_link(Host, []),
替换为{ok, Conn} = mongo_connection:start_link(Host, []),