Anonymous avatar Anonymous committed 30a5490 Merge

count and distinct commands
Merge from http://bitbucket.org/japerk/emongo/

Comments (0)

Files changed (4)

 {application, emongo, [
 	{description, "Erlang MongoDB Driver"},
-	{vsn, "0.0.4"},
+	{vsn, "0.0.5"},
 	{modules, [
 		emongo, emongo_app, emongo_sup, emongo_bson, emongo_packet,
 		emongo_server, emongo_pool, emongo_collection

ebin/emongo.appup

-{"0.0.4", [
+{"0.0.5", [
+	{"0.0.4", [
+		{load_module, emongo_bson},
+		{load_module, emongo}
+	]},
 	{"0.0.3", [{add_module, emongo_collection}]},
 	{"0.0.2", [
 		{load_module, emongo},
 		{apply, {emongo_app, initialize_pools, []}}
 	]}
 ], [
+	{"0.0.4", [
+		{load_module, emongo_bson},
+		{load_module, emongo}
+	]},
 	{"0.0.3", [{delete_module, emongo_collection}]},
 	{"0.0.2", [
 		{load_module, emongo},
          find_all/2, find_all/3, find_all/4,
          find_one/3, find_one/4,
          insert/3, update/4, update/5, delete/2, delete/3,
-         ensure_index/3, count/2, dec2hex/1, hex2dec/1]).
+		 ensure_index/3, count/2, count/3, distinct/3, distinct/4,
+		 dec2hex/1, hex2dec/1]).
 
 -export([update_sync/5, delete_sync/3]).
 
 	Packet = emongo_packet:ensure_index(Database, Collection, ReqId, Keys),
 	emongo_server:send(Pid, ReqId, Packet).
 
-count(PoolId, Collection) ->
+
+count(PoolId, Collection) -> count(PoolId, Collection, []).
+
+
+count(PoolId, Collection, Selector) ->
 	{Pid, Database, ReqId} = get_pid_pool(PoolId),
-	Query = #emo_query{q=[{<<"count">>, Collection}, {<<"ns">>, Database}], limit=1},
+	Q = [{<<"count">>, Collection}, {<<"ns">>, Database},
+		 {<<"query">>, transform_selector(Selector)}],
+	Query = #emo_query{q=Q, limit=1},
 	Packet = emongo_packet:do_query(Database, "$cmd", ReqId, Query),
 	case emongo_server:send_recv(Pid, ReqId, Packet, ?TIMEOUT) of
 		#response{documents=[[{<<"n">>,Count}|_]]} ->
 			undefined
 	end.
 
+
+distinct(PoolId, Collection, Key) -> distinct(PoolId, Collection, Key, []).
+
+distinct(PoolId, Collection, Key, Selector) ->
+	{Pid, Database, ReqId} = get_pid_pool(PoolId),
+	Q = [{<<"distinct">>, Collection}, {<<"key">>, Key}, {<<"ns">>, Database},
+		 {<<"query">>, transform_selector(Selector)}],
+	Query = #emo_query{q=Q, limit=1},
+	Packet = emongo_packet:do_query(Database, "$cmd", ReqId, Query),
+	case emongo_server:send_recv(Pid, ReqId, Packet, ?TIMEOUT) of
+		#response{documents=[[{<<"values">>, {array, Vals}} | _]]} ->
+			Vals;
+		_ ->
+			undefined
+	end.
+
+
 %drop_collection(PoolId, Collection) when is_atom(PoolId), is_list(Collection) ->
 
 %%====================================================================

src/emongo_collection.erl

 
 -include("emongo.hrl").
 
+-export([distinct_set/4, distinct_list/4]).
 -export([fold/4, fold/5, fold/6]).
 -export([foreach/3, foreach/4, foreach/5]).
 
+%%%%%%%%%%%%%%
+%% distinct %%
+%%%%%%%%%%%%%%
+
+distinct_set(PoolId, Collection, Key, Selector) ->
+	F = fun({array, List}, Set) -> sets:union(Set, sets:from_list(List)) end,
+	Arrays = emongo:distinct(PoolId, Collection, Key, Selector),
+	lists:foldl(F, sets:new(), Arrays).
+
+distinct_list(PoolId, Collection, Key, Selector) ->
+	lists:sort(sets:to_list(distinct_set(PoolId, Collection, Key, Selector))).
+
 %%%%%%%%%%
 %% fold %%
 %%%%%%%%%%
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.