Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
ebin/*.beam
ebin/*.app
.eunit
12 changes: 7 additions & 5 deletions Emakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{"src/*", [
debug_info,
{i, "include/"},
{outdir, "ebin/"}
]}.
% -*- mode: erlang -*-

{[ 'src/*' ],
[ {i, "include"},
{outdir, "ebin"},
debug_info ]
}.
39 changes: 8 additions & 31 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,36 +1,13 @@
PKGNAME=emongo
ROOTDIR=`erl -eval 'io:format("~s~n", [code:root_dir()])' -s init stop -noshell`
LIBDIR=$(shell erl -eval 'io:format("~s~n", [code:lib_dir()])' -s init stop -noshell)
# get application vsn from app file
VERSION=$(shell erl -pa ebin/ -eval 'application:load(${PKGNAME}), {ok, Vsn} = application:get_key(${PKGNAME}, vsn), io:format("~s~n", [Vsn])' -s init stop -noshell)
all: emake

all: src
emake:
erl -make

src: FORCE
@erl -make
@cp src/${PKGNAME}.app.src ebin/${PKGNAME}.app

test: src
test: emake
prove t/*.t

clean:
rm -rf erl_crash.dump *.boot *.rel *.script ebin/*.beam ebin/emongo.app

package: clean
@mkdir $(PKGNAME)-$(VERSION)/ && cp -rf ebin include Emakefile Makefile priv README.markdown src t $(PKGNAME)-$(VERSION)
@COPYFILE_DISABLE=true tar zcf $(PKGNAME)-$(VERSION).tgz $(PKGNAME)-$(VERSION)
@rm -rf $(PKGNAME)-$(VERSION)/

install: src
@mkdir -p $(DESTDIR)/$(LIBDIR)/$(PKGNAME)-$(VERSION)/ebin
@mkdir -p $(DESTDIR)/$(LIBDIR)/$(PKGNAME)-$(VERSION)/include
for i in ebin/*.beam include/*.hrl ebin/*.app; do install $$i $(DESTDIR)/$(LIBDIR)/$(PKGNAME)-$(VERSION)/$$i ; done
check: emake
@./rebar eunit skip_deps=true


plt: src
@dialyzer --check_plt -q -r . -I include/

check: src
@dialyzer --src -r . -I include/

FORCE:
clean:
rm -rf $(wildcard ebin/*.beam) erl_crash.dump .eunit/
148 changes: 65 additions & 83 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
#### The goal of emongo is to be stable, fast and easy to use.

## Compile and install
## Build

make
sudo make install


## Start emongo

application:start(emongo).

## Connecting to mongodb

#### Option 1 - Config file

example.config:

[{emongo, [
{pools, [
{pool1, [
Expand All @@ -25,25 +24,25 @@ example.config:
]}
]}
]}].

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

__PoolName__ = atom()
__PoolId__ = atom()
__Host__ = string()
__Port__ = integer()
__Database__ = string()
Expand All @@ -53,63 +52,61 @@ __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>
__BinSubType__ = integer() <http://www.mongodb.org/display/DOCS/BSON#BSON-noteondatabinary>

## Add Pool

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

## Insert

__PoolName__ = atom()
__PoolId__ = atom()
__CollectionName__ = string()
__Document__ = [{Key, Val}]
__Documents__ = [Document]

emongo:insert(PoolName, CollectionName, Document) -> ok
emongo:insert(PoolName, CollectionName, Documents) -> ok
__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"}]]).
emongo:insert(test, "collection", [[{"document1_field1", "value1"}], [{"document2_field1", "value1"}]]).

## Update

__PoolName__ = atom()
__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)
__MultiUpdate__ = true | false (if all documents matching selector should be updated)

%% by default upsert == false and multiupdate == false
emongo:update(PoolName, CollectionName, Selector, Document) -> ok
emongo:update(PoolName, CollectionName, Selector, Document, Upsert) -> ok
emongo:update(PoolName, CollectionName, Selector, Document, Upsert, MultiUpdate) -> ok

%% 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

__PoolName__ = atom()
__PoolId__ = atom()
__CollectionName__ = string()
__Selector__ = Document
__Selector__ = Document

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

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

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
Expand All @@ -118,12 +115,12 @@ __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_all(PoolName, CollectionName) -> Result
emongo:find_all(PoolName, CollectionName, Selector) -> Result
emongo:find_all(PoolName, CollectionName, Selector, Options) -> Result

__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__
Expand All @@ -132,81 +129,66 @@ __limit, offset, timeout, orderby, fields__
%% 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_all(test, "collection", [{"field1", 1}], [{limit, 100}, {offset, 10}, {timeout, 5000}, {orderby, [{"field1", asc}]}, {fields, ["field1"]}]).

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_all(test, "collection", [{"field1", [{gt, 5}, {lt, 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_all(test, "collection", [{"field1", [{gte, 5}, {lte, 10}]}]).

emongo:find(test, "collection", [{"field1", [{gte, 5}, {lte, 10}]}]).
%% find documents where field1 is greater than 5 and less than 10
emongo:find_all(test, "collection", [{"field1", [{'>', 5}, {'<', 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_all(test, "collection", [{"field1", [{'>=', 5}, {'=<', 10}]}]).

emongo:find(test, "collection", [{"field1", [{'>=', 5}, {'=<', 10}]}]).
__not equal__

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

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

emongo:find(test, "collection", [{"field1", [{'=/=', 5}]}]).
%% find documents where field1 is not equal to 5
emongo:find_all(test, "collection", [{"field1", [{'/=', 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_all(test, "collection", [{"field1", [{in, [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_all(test, "collection", [{"field1", [{nin, [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_all(test, "collection", [{"field1", [{all, [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_all(test, "collection", [{"field1", [{size, 10}]}]).

emongo:find(test, "collection", [{"field1", [{size, 10}]}]).
__exists__

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

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

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

emongo:find(test, "collection", [{where, "this.field1 > 10"}]).
__nested queries__

%% find documents with an address field containing a sub-document
%% 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_all(test, "people", [{"address.street", "Maple Drive"}]).

## Drop database

%% drop current database
emongo:drop_database(PoolName) -> ok

## Tests

Ensure you have [etap](https://github.com/ngerakines/etap).

git clone https://github.com/ngerakines/etap.git
cd etap && make && cd ..
export ERL_LIBS="etap"

make test
emongo:find(test, "people", [{"address.street", "Maple Drive"}]).
12 changes: 0 additions & 12 deletions debian/changelog

This file was deleted.

1 change: 0 additions & 1 deletion debian/compat

This file was deleted.

13 changes: 0 additions & 13 deletions debian/control

This file was deleted.

1 change: 0 additions & 1 deletion debian/copyright

This file was deleted.

5 changes: 0 additions & 5 deletions debian/rules

This file was deleted.

14 changes: 14 additions & 0 deletions ebin/emongo.app
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{application, emongo, [
{description, "Erlang MongoDB Driver"},
{vsn, "0.2"},
{modules, [
emongo,
emongo_app,
emongo_bson,
emongo_conn,
emongo_packet
]},
{registered, []},
{mod, {emongo_app, []}},
{applications, [kernel, stdlib, sasl]}
]}.
Loading