Commits

Jacob Vorreuter  committed f170ade

adding options to emongo:find and adding api docs to readme

  • Participants
  • Parent commits 6707f29

Comments (0)

Files changed (2)

File README.markdown

-Emongo is still under development and lacking in functionality.  The goal of emongo, however, is to be stable, fast and easy to use.
+#### 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:
+	
+	[{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
+
+#### Types
+
+	PoolName = 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(PoolName, Host, Port, Database, PoolSize) -> 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(PoolName, CollectionName) -> Result
+	emongo:find(PoolName, CollectionName, Selector) -> Result
+	emongo:find(PoolName, CollectionName, Selector, Options) -> Result

File src/emongo.erl

 %%		 Collection = string()
 %%		 Selector = document()
 %%		 Options = [Option]
-%%		 Option = {timeout, Timeout} | response_options
+%%		 Option = {timeout, Timeout} | {limit, Limit} | {offset, Offset} | {orderby, Orderby} | {fields, Fields} | response_options
 %%		 Timeout = integer (timeout in milliseconds)
+%%		 Limit = integer
+%%		 Offset = integer
+%%		 Orderby = [{Key, Direction}]
+%%		 Key = string() | binary() | atom() | integer()
+%%		 Direction = 1 (Asc) | -1 (Desc)
+%%		 Fields = [Field]
+%%		 Field = string() | binary() | atom() | integer() = specifies a field to return in the result set
 %%		 response_options = return {response, header, response_flag, cursor_id, offset, limit, documents}
 %%		 Result = documents() | response()
 find(PoolId, Collection, Selector, Options) when ?IS_DOCUMENT(Selector) ->
 	{Pid, Pool} = gen_server:call(?MODULE, {pid, PoolId}, infinity),
-	Query = create_query(Selector, Options),
+	Query = create_query(Options, #emo_query{q=Selector}),
 	Packet = emongo_packet:do_query(Pool#pool.database, Collection, Pool#pool.req_id, Query),
-	emongo_conn:send_recv(Pid, Pool#pool.req_id, Packet, proplists:get_value(timeout, Options, ?TIMEOUT)).
+	Resp = emongo_conn:send_recv(Pid, Pool#pool.req_id, Packet, proplists:get_value(timeout, Options, ?TIMEOUT)),
+	case lists:member(response_options, Options) of
+		true -> Resp;
+		false -> Resp#response.documents
+	end.
 
+create_query([], Query) ->
+	Query;
+	
+create_query([{limit, Limit}|Options], Query) ->
+	Query1 = Query#emo_query{limit=Limit},
+	create_query(Options, Query1);
+	
+create_query([{offset, Offset}|Options], Query) ->
+	Query1 = Query#emo_query{offset=Offset},
+	create_query(Options, Query1);
+
+create_query([{orderby, Orderby}|Options], Query) ->
+	Selector = Query#emo_query.q,
+	Query1 = Query#emo_query{q=[{<<"orderby">>, Orderby}|Selector]},
+	create_query(Options, Query1);
+	
+create_query([{fields, Fields}|Options], Query) ->
+	Query1 = Query#emo_query{field_selector=[{Field, 1} || Field <- Fields]},
+	create_query(Options, Query1);
+	
+create_query([_|Options], Query) ->
+	create_query(Options, Query).
+	
 %%------------------------------------------------------------------------------
 %% find_all
 %%------------------------------------------------------------------------------
 get_pool(PoolId, [Pool|Tail], Others) ->
 	get_pool(PoolId, Tail, [Pool|Others]).
 	
-create_query(Selector, _Options) ->
-	#emo_query{q=Selector}.
-
 dec2hex(Dec) ->
 	dec2hex(<<>>, Dec).