首页 文章

mongodb-erlang Erlang驱动程序示例

提问于
浏览
4

我在哪里可以找到mongodb-erlang用法的例子?我找到的唯一信息来源是这个文件:https://github.com/TonyGen/mongodb-erlang/blob/master/src/mongodb_tests.erl但它不包括许多基本查询,如下面的内容(从MongoDB网站中选取):

db.collection.find().sort({name : 1, age: -1}).limit(10);
db.users.find().skip(20).limit(10);
db.things.ensureIndex({j:1});
db.things.find({colors : {$ne : "red"}});
db.collection.find({ "field" : { $gte: value } } );
db.things.find( { "x.a" : 1, "x.b" : { $gt : 1 } } )

那么如何根据erlang驱动程序编写这些查询?

2 回答

  • 2

    对于官方支持的驱动程序,API在此处记录:

    http://api.mongodb.org/erlang/mongodb/

    对于查找操作,请查看函数索引,具体如下:

    http://api.mongodb.org/erlang/mongodb/mongo.html#find-2

    如果这不符合您的口味,您可能还想查看社区驱动程序:

    emongo / erlmongo - 他们各自的自述文件和文档也有一些例子:

    https://bitbucket.org/rumataestor/emongo

    https://github.com/wpntv/erlmongo

  • 1

    我也有类似的问题,这是我对官方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可以为您提供更多详细信息 .

    • 我是一个二郎新手,因为找到上面的用法花了我一些时间,希望这对你有帮助:)

相关问题