Skip to content

Latest commit

 

History

History
240 lines (164 loc) · 7.4 KB

README.markdown

File metadata and controls

240 lines (164 loc) · 7.4 KB

Emongo

MongoDB driver for ejabberd.

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

Compile and install

make
sudo make install

Start emongo

application:start(emongo).

Connecting to mongodb

Option 1 - Config file

example.config: Single mongo instance

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

repl_example.config: Replication sets, see: http://www.mongodb.org/display/DOCS/Replica+Sets

[{emongo, [
	{pools, [
		{repl1, [
			{size, 1},
			{urls, [{"localhost",27018},{"localhost",27019}]},
			{database, "testdatabase"}
		]}
	]}
]}].

specify the config file path when starting Erlang

Standalone:

erl -config priv/example

Replication Set:

erl -config priv/repl_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).
%% Add a standalone pool
emongo:add_pool(pool1, "localhost", 27017, "testdatabase", 1).
%% add a replica set
emogo:add_pool(repl1, ["localhost:27018","localhost:27019"], "testdatabase", 1).

API Type Reference

PoolName = atom()
URL = string() | {string(),integer()}
URLs = [URL]
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(PoolName, Host, Port, Database, PoolSize) -> ok

emongo:add_pool(PoolName, URLs, Database, PoolSize) -> ok

Insert

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

emongo:insert(PoolName, CollectionName, Document) -> ok
emongo:insert(PoolName, 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

PoolName = 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

Examples

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

Delete

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

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

%% delete all documents in a collection that match a selector
emongo:delete(PoolName, 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_all(PoolName, CollectionName) -> Result
emongo:find_all(PoolName, CollectionName, Selector) -> Result
emongo:find_all(PoolName, 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_all(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}]}]).

%% 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}]}]).

%% find documents where field1 is greater than 5 and less than 10
emongo:find_all(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}]}]).

not equal

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

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

%% find documents where field1 is not equal to 5
emongo:find_all(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]}]}]).

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]}]}]).

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]}]}]).

size

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

exists

%% find documents where field1 exists
emongo:find_all(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"}]).

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_all(test, "people", [{"address.street", "Maple Drive"}]).

Drop database

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

Tests

Ensure you have etap.

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

make test