Coder Social home page Coder Social logo

emongo's Introduction

The goal of emongo is to be stable, fast and easy to use.

Build

make

Start emongo

application:start(emongo).

Connecting to mongodb

Option 1 - Config file

example.config:

[{emongo, [
	{pools, [
		{pool1, [
			{size, 1},
			{host, "localhost"},
			{port, 27017},
			{database, "testdatabase"}
		]}
	]}
]}].

specify the config file path when starting Erlang

erl -config priv/example

start the application

application:start(emongo).

Option 2 - Add pool

start the app and then add as many pools as you like

application:start(emongo).
emongo:add_pool(pool1, "localhost", 27017, "testdatabase", 1).

API Type Reference

PoolId = atom()
Host = string()
Port = integer()
Database = string()
PoolSize = integer()
CollectionName = string()
Selector = Document
Document = [{Key, Val}]
Key = string() | atom() | binary() | integer()
Val = float() | string() | binary() | Document | {array, [term()]} | {binary, BinSubType, binary()} | {oid, binary()} | {oid, string()} | bool() | now() | datetime() | undefined | {regexp, string(), string()} | integer()
BinSubType = integer() http://www.mongodb.org/display/DOCS/BSON#BSON-noteondatabinary

Add Pool

emongo:add_pool(PoolId, Host, Port, Database, PoolSize) -> ok

Insert

PoolId = atom()
CollectionName = string()
Document = [{Key, Val}]
Documents = [Document]

emongo:insert(PoolId, CollectionName, Document) -> ok
emongo:insert(PoolId, CollectionName, Documents) -> ok

Examples

%% insert a single document with two fields into the "collection" collection
emongo:insert(test, "collection", [{"field1", "value1"}, {"field2", "value2"}]).

%% insert two documents, each with a single field into the "collection" collection
emongo:insert(test, "collection", [[{"document1_field1", "value1"}], [{"document2_field1", "value1"}]]).	

Update

PoolId = atom()
CollectionName = string()
Selector = Document
Document = [{Key, Val}]
Upsert = true | false (insert a new document if the selector does not match an existing document)

%% by default upsert == false
emongo:update(PoolId, CollectionName, Selector, Document) -> ok
emongo:update(PoolId, CollectionName, Selector, Document, Upsert) -> ok

Examples

%% update the document that matches "field1" == "value1"
emongo:update(test, "collection", [{"field1", "value1"}], [{"field1", "value1"}, {"field2", "value2"}]).

Delete

PoolId = atom()
CollectionName = string()
Selector = Document

%% delete all documents in a collection
emongo:delete(PoolId, CollectionName) -> ok

%% delete all documents in a collection that match a selector
emongo:delete(PoolId, CollectionName, Selector) -> ok

Find

Options = {timeout, Timeout} | {limit, Limit} | {offset, Offset} | {orderby, Orderby} | {fields, Fields} | response_options
Timeout = integer (timeout in milliseconds)
Limit = integer
Offset = integer
Orderby = [{Key, Direction}]
Direction = 1 (Asc) | -1 (Desc)
Fields = [Key] = specifies a list of fields to return in the result set
response_options = return #response{header, response_flag, cursor_id, offset, limit, documents}
Result = [Document] | response()

emongo:find(PoolId, CollectionName) -> Result
emongo:find(PoolId, CollectionName, Selector) -> Result
emongo:find(PoolId, CollectionName, Selector, Options) -> Result

Examples

limit, offset, timeout, orderby, fields

%% find documents from 'collection' where field1 equals 1 and abort the query if it takes more than 5 seconds
%% limit the number of results to 100 and offset the first document 10 documents from the beginning
%% return documents in ascending order, sorted by the value of field1
%% limit the fields in the return documents to field1 (the _id field is always included in the results)
emongo:find(test, "collection", [{"field1", 1}], [{limit, 100}, {offset, 10}, {timeout, 5000}, {orderby, [{"field1", asc}]}, {fields, ["field1"]}]).

great than, less than, great than or equal, less than or equal

%% find documents where field1 is greater than 5 and less than 10
emongo:find(test, "collection", [{"field1", [{gt, 5}, {lt, 10}]}]).
  
%% find documents where field1 is greater than or equal to 5 and less than or equal to 10
emongo:find(test, "collection", [{"field1", [{gte, 5}, {lte, 10}]}]).
  
%% find documents where field1 is greater than 5 and less than 10
emongo:find(test, "collection", [{"field1", [{'>', 5}, {'<', 10}]}]).
  
%% find documents where field1 is greater than or equal to 5 and less than or equal to 10
emongo:find(test, "collection", [{"field1", [{'>=', 5}, {'=<', 10}]}]).

not equal

%% find documents where field1 is not equal to 5 or 10
emongo:find(test, "collection", [{"field1", [{ne, 5}, {ne, 10}]}]).
  
%% find documents where field1 is not equal to 5
emongo:find(test, "collection", [{"field1", [{'=/=', 5}]}]).
  
%% find documents where field1 is not equal to 5
emongo:find(test, "collection", [{"field1", [{'/=', 5}]}]).

in

%% find documents where the value of field1 is one of the values in the list [1,2,3,4,5]
emongo:find(test, "collection", [{"field1", [{in, [1,2,3,4,5]}]}]).

not in

%% find documents where the value of field1 is NOT one of the values in the list [1,2,3,4,5]
emongo:find(test, "collection", [{"field1", [{nin, [1,2,3,4,5]}]}]).

all

%% find documents where the value of field1 is an array and contains all of the values in the list [1,2,3,4,5]
emongo:find(test, "collection", [{"field1", [{all, [1,2,3,4,5]}]}]).

size

%% find documents where the value of field1 is an array of size 10
emongo:find(test, "collection", [{"field1", [{size, 10}]}]).

exists

%% find documents where field1 exists
emongo:find(test, "collection", [{"field1", [{exists, true}]}]).

where

%% find documents where the value of field1 is greater than 10
emongo:find(test, "collection", [{where, "this.field1 > 10"}]).

nested queries

%% find documents with an address field containing a sub-document 
%% with street equal to "Maple Drive".
%% ie: [{"address", [{"street", "Maple Drive"}, {"zip", 94114}]
emongo:find(test, "people", [{"address.street", "Maple Drive"}]).

emongo's People

Contributors

jkvor avatar vkoivula avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

emongo's Issues

application not starting

2> application:start(emongo).

=PROGRESS REPORT==== 8-Aug-2011::08:42:18 ===
supervisor: {local,inet_gethost_native_sup}
started: [{pid,<0.51.0>},{mfa,{inet_gethost_native,init,[[]]}}]

=PROGRESS REPORT==== 8-Aug-2011::08:42:18 ===
supervisor: {local,kernel_safe_sup}
started: [{pid,<0.50.0>},
{name,inet_gethost_native_sup},
{mfa,{inet_gethost_native,start_link,[]}},
{restart_type,temporary},
{shutdown,1000},
{child_type,worker}]
ok
3>
=PROGRESS REPORT==== 8-Aug-2011::08:42:18 ===
supervisor: {local,emongo_app}
started: [{pid,<0.48.0>},
{name,emongo},
{mfa,{emongo,start_link,[]}},
{restart_type,permanent},
{shutdown,5000},
{child_type,worker}]

=PROGRESS REPORT==== 8-Aug-2011::08:42:18 ===
application: emongo
started_at: nonode@nohost

=CRASH REPORT==== 8-Aug-2011::08:42:18 ===
crasher:
initial call: emongo_conn:init/4
pid: <0.49.0>
registered_name: []
exception error: no function clause matching
emongo_packet:decode_response(<<"RFB 003.008\n">>)
in function emongo_conn:loop/2
ancestors: [emongo,emongo_app,<0.46.0>]
messages: []
links: [<0.48.0>,#Port<0.650>]
dictionary: []
trap_exit: false
status: running
heap_size: 377
stack_size: 24
reductions: 386
neighbours:

Further Support Reqd for Nested queries

How does one implement the following nest query (for Geospatial indexed queries)

db.collection.find( {"loc":{$near:[80.9, 89.8],$maxDistance:0.00039240307}})

Find by BSON

How I can create an selector to find by id field?

Like this:

emongo:find(test, "collection", [{"field1", my_id}]).

or

emongo:find(test, "collection", [{where, "this.field1 == my_id"}]).

How to run commands such as 'shardCollection' using emongo?

Hi,
First of all sorry if this is not the right place to post questions like these. I am trying to shard a collection using emongo but am not sure how to run a command using the emongo driver. Could someone shed some light on it and tell if that is even possible.
Thanks,
Vishal

pushing some integers encodes as string representation of chars

in place updates like this:

emongo:update(pool1, "coll1", [{"key", 1}], [{"$push", [{"f3", 11}, {"f4", 12}]}], true)

return this:

[[{<<"_id">>,
{oid,<<75,143,234,245,115,251,145,60,197,30,214,253>>}},
{<<"key">>,1},
{<<"f3">>,{array,"\v"}},
{<<"f4">>,{array,"\f"}}]]

11 is vertical tab and 12 form feed in decimal. Have only seen this with the numbers 11, 12, and 13 (which yields "\r"). So for example, 15 encodes fine as an int. And presumably so do many other numbers.... :)

Need to start sasl before starting emongo

maybe this is a documentation bug, if I try to start emongo like this:

1> application:start(emongo).
{error,{not_started,sasl}}

if I start the shell with this options:

erl -pa emongo/ebin -sasl errlog_type error -eval "application:start(sasl)"

then:

1> application:start(emongo).
ok

I think it should be in the REAME (or maybe there is something I'm missing)

make install fails on fedora 11

After running a fresh make, I try to run sudo make install and get a bunch of errors trying to find erl. The erl and erlc executables in the /usr/local/ search path and can be found when running them with sudo as is.

[ngerakines@localhost jacobvorreuter-emongo]$ make
sh ebin/emongo.app.in 0.0.1
mkdir -p ebin/
(cd src;make)
make[1]: Entering directory `/home/ngerakines/code/jacobvorreuter-emongo/src'
erlc -W -I ../include -pa ../ebin +debug_info -o ../ebin emongo_app.erl
erlc -W -I ../include -pa ../ebin +debug_info -o ../ebin emongo_bson.erl
erlc -W -I ../include -pa ../ebin +debug_info -o ../ebin emongo_conn.erl
erlc -W -I ../include -pa ../ebin +debug_info -o ../ebin emongo.erl
erlc -W -I ../include -pa ../ebin +debug_info -o ../ebin emongo_packet.erl
make[1]: Leaving directory `/home/ngerakines/code/jacobvorreuter-emongo/src'
erl -pa ebin -noshell -run emongo_app build_rel -s init stop
[ngerakines@localhost jacobvorreuter-emongo]$ ls
ebin  emongo.boot  emongo.rel  emongo.script  include  Makefile  priv  README.markdown  src  support  t
[ngerakines@localhost jacobvorreuter-emongo]$ sudo make install
/bin/sh: erl: command not found
/bin/sh: erl: command not found
/bin/sh: erl: command not found
for i in ebin/*.beam include/*.hrl ebin/*.app; do install $i /`erl -eval 'io:format("~s~n", [code:lib_dir()])' -s init stop -noshell`/emongo-0.0.1/$i ; done
/bin/sh: erl: command not found
/bin/sh: erl: command not found
/bin/sh: erl: command not found
/bin/sh: erl: command not found
/bin/sh: erl: command not found
/bin/sh: erl: command not found
/bin/sh: erl: command not found
cp *.boot /`erl -eval 'io:format("~s~n", [code:root_dir()])' -s init stop -noshell`/bin/
/bin/sh: erl: command not found

There's nothing funky or special about my Erlang install. It was installed from source with the standard ./configure && make && sudo make install/.

License ?

Hi,

I didn't find any license file in the repository.

Add ability to do Atomic increments

Currently you cannot do something like
emongo:update(stats, "test_stats", [{"val", "true"}], [{"val", "true"}, {inc, [{"hits",1}], true).
to do an atomic upsert.

array of integers

emonogo turns a list of integers to a binary string. Probably because a string is a list in erlang.
Maybe emongo should except strings only as binaries and list of integer will be a list of integers?

In the shell
db.user.insert({k1: "v1", "k2": [1,2]})
db.user.find()
{ "_id" : ObjectId("4b7e885bf1e7e6a4f6ac5193"), "k1" : "v1", "k2" : [ 1, 2 ] }

With emongo:
emongo:insert(<<"mydb">>, "user", [{"k1", "v1"}, {"k2", [1, 2]}]).
emongo:find(<<"mydb">>, "user").
[[{<<"_id">>,
{oid,<<75,126,137,24,228,24,138,65,245,88,191,247>>}},
{<<"k1">>,<<"v1">>},
{<<"k2">>,<<1,2>>}]]

bson does not properly decode _id value

from shell:

19> emongo:find(db_pool,"users",[{"_id",{oid,"4ba503889a221a453f46e02e"}}]). [[{<<"_id">>,
{oid,<<75,165,3,136,154,34,26,69,63,70,224,46>>}},
{<<"date_added">>,{2010,3,0}},
{<<"name">>,<<"Olo">>},
{<<"number">>,<<"235626">>},
{<<"password">>,<<"AAA">>}]]

"_id" should be decoded to:

24> emongo:dec2hex(<<75,165,3,136,154,34,26,69,63,70,224,46>>).
<<"4ba503889a221a453f46e02e">>

at very least.

A single timeout seems to cause perpetual timeouts

I've got a grid of 6 nodes/servers running emongo with 20 connections each to a mongodb server. One of them will 'trip' and a timeout exception will raise causing all subsequent emongo_conn:send_recv/4 to also throw that timeout exception. Once it gets into this mode the only way to resolve the issue is to stop/start the emongo app (and any apps that depend on it).

exception exit: timeout
  in function  gen:wait_resp_mon/3
  in call from emongo_conn:send_recv/4
  in ...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.