简体   繁体   English

mongodb-erlang Erlang驱动程序示例

[英]mongodb-erlang Erlang driver examples

Where i can find examples of mongodb-erlang usage ? 我在哪里可以找到mongodb-erlang用法的例子? The only source of information i found is this file: https://github.com/TonyGen/mongodb-erlang/blob/master/src/mongodb_tests.erl But it not covers many of basic queries like following (picked from MongoDB site): 我找到的唯一信息来源是这个文件: 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 } } )

So how to write these queries in terms of erlang driver ? 那么如何根据erlang驱动程序编写这些查询?

For the officially supported driver the API is documented here: 对于官方支持的驱动程序,API在此处记录:

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

For the find operations check out the function index, specifically: 对于查找操作,请查看函数索引,具体如下:

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

If that is not to your taste, you may want to also check out the community drivers: 如果这不符合您的口味,您可能还想查看社区驱动程序:

emongo/erlmongo - their respective READMEs and docs have some examples too: emongo / erlmongo - 他们各自的自述文件和文档也有一些例子:

https://bitbucket.org/rumataestor/emongo https://bitbucket.org/rumataestor/emongo

https://github.com/wpntv/erlmongo https://github.com/wpntv/erlmongo

I have this similar issues too, here is my resolution with the offical mongodb-erlang driver: 我也有类似的问题,这是我对官方mongodb-erlang驱动程序的解决方案:

1.The test mongodb records: 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.How to implement "db.erltest.find({x:{$gt:2}})" via mongodb-erlang? 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.

Summaries: 摘要:

  1. Please ensure include the correct path with the *mongo_protocol.hrl* file. 请确保使用* mongo_protocol.hrl *文件包含正确的路径。
  2. Cursor = mongo_query:find(DbConn, #'query'{collection=erltest, selector={x, {'$gt', 2}}}) is the detailed implement. Cursor = mongo_query:find(DbConn, #'query'{collection=erltest, selector={x, {'$gt', 2}}})是详细的工具。
  3. Maybe mongodb_test.erl can supply more details for you. 也许mongodb_test.erl可以为您提供更多细节。
  4. I'm an erlang newbie, for finding the above usage take me some time, hope this is helpful for you :) 我是一个二郎新手,因为找到上面的用法花了我一些时间,希望这对你有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM