Closed yaocl closed 4 years ago
please check this test. Aggregate is being used there.
Thx for your reply. I had worked out the code.
QueryResult = mongoc:transaction(?MONGO_POOL,
fun( #{pool := Worker} ) ->
%% lager:debug("Worker=~p", [Worker]),
mc_worker_api:command(Worker,
{
<<"aggregate">>, ?COL_CHATMSG,
<<"pipeline">>, [
{<<"$match">>,
{<<"domain">>, <<"charleystg">>}
},
{<<"$unwind">>, <<"$from">>},
{<<"$group">>, {
<<"_id">>, <<"$from">>,
<<"time">>, {
<<"$sum">>, 1
}
}},
{<<"$sort">>, {<<"time">>, -1}}
],
<<"cursor">>, { <<"batchSize">>, 20 }
})
end),
%% lager:debug("QueryResult = ~p", [QueryResult]),
R1 = case QueryResult of
{ok, Cursor} ->
Res = mc_cursor:rest(Cursor),
%% lager:debug("Cursor=~p", [Cursor]),
lager:debug("Res=~p", [Res]),
lists:foreach(
fun(Batch) ->
Id = maps:get(<<"_id">>, Batch),
Time = maps:get(<<"time">>, Batch),
lager:debug("Id=~p, Time=~p", [Id, Time])
end, Res
),
Res;
{error, Error} ->
lager:debug("Error=~p", [Error]),
[]
end,
R1.
Any examples to do distinct and aggregate command ?