我也有类似的问题,这是我对官方 mongodb-erlang 驱动程序的解决方案:
1.测试mongodb记录:
>db.erltest.find()
{ "_id" : ObjectId("4fe80d692f6cc055a32da380"), "x" : 1, "y" : 2 }
{ "_id" : ObjectId("4fe80d702f6cc055a32da381"), "x" : 2, "y" : 3 }
{ "_id" : ObjectId("4fe80d762f6cc055a32da382"), "x" : 10, "y" : 3 }
{ "_id" : ObjectId("4fe80d7e2f6cc055a32da383"), "x" : 10, "y" : 4 }
2.如何通过mongodb-erlang实现“db.erltest.find({x:{$gt:2}})”?
-module(mongo_test2).
-export([tmp_test/0]).
-include ("/opt/Erlang/lib/erlang/lib/mongodb-master/include/mongo_protocol.hrl").
tmp_test() ->
application:start(mongodb),
Host = {localhost, 27017},
{ok, Conn} = mongo:connect(Host),
io:format("Conn is : ~p~n", [Conn]),
DbConn = {test, Conn},
Cursor = mongo_query:find(DbConn, #'query'{collection=erltest, selector={x, {'$gt', 2}}}),
process(Cursor),
mongo:disconnect(Conn).
process({}) ->
ok;
process(Cursor) ->
io:format("----Cursor:~p~n", [Cursor]),
Record = mongo:next(Cursor),
io:format("Record:~p~n", [Record]),
case Record of
{} ->
no_more;
_ ->
process(Cursor)
end.
总结:
- 请确保在 *mongo_protocol.hrl* 文件中包含正确的路径。
-
Cursor = mongo_query:find(DbConn, #'query'{collection=erltest, selector={x, {'$gt', 2}}}) 是详细工具。
- 也许mongodb_test.erl 可以为您提供更多详细信息。
- 我是一个erlang新手,找上面的用法要花点时间,希望对你有帮助:)