Erlang driver examples from mongodb-erlang

Where can I find examples of using 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 does not cover many basic queries, such as the following (selected on the site 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 the erlang driver?

+4
source share
2 answers

For an officially supported driver, the API is documented here:

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

For search operations, check the function index, in particular:

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

If this is not to your liking, you can also check the community drivers:

emongo/erlmongo - README :

https://bitbucket.org/rumataestor/emongo

https://github.com/wpntv/erlmongo

+2

, 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. "db.erltest.find({x: {$ gt: 2}})" mongodb-erlang?

-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 .
  • -, , , , , :)
+1

All Articles