MongoDB Erlang驱动程序查找限制发生在哪里?

MongoDB Erlang驱动程序查找限制发生在哪里?,mongodb,erlang,cursor,Mongodb,Erlang,Cursor,我正在尝试在Erlang中集成MongoDB驱动程序 在一些编码之后,我觉得限制检索行数的唯一方法只能在find()操作之后处理光标时出现 以下是我目前的代码: Cursor = mongo:find(Connection, Collection, Selector), Result = case Limit of infinity -> mc_cursor:rest(Cursor); _ ->

我正在尝试在Erlang中集成MongoDB驱动程序

在一些编码之后,我觉得限制检索行数的唯一方法只能在
find()
操作之后处理光标时出现

以下是我目前的代码:

Cursor = mongo:find(Connection, Collection, Selector),
Result = case Limit of
              infinity ->
                 mc_cursor:rest(Cursor);
              _ ->
                 mc_cursor:take(Cursor, Limit)
         end,
mc_cursor:close(Cursor)
  • 我担心的是,当收藏量巨大时,会发生什么
  • 获取和匹配内存不是太大了吗
  • 光标基本上是如何工作的
  • 还是有更好的方法来限制提取

我想您可以使用
批量大小
参数。 以下代码来自
mongo.erl
文件

%% @doc Return projection of selected documents starting from Nth document in batches of batchsize.
%%      0 batchsize means default batch size.
%%      Negative batch size means one batch only.
%%      Empty projection means full projection.
-spec find(pid(), collection(), selector(), projector(), skip(), batchsize()) -> cursor(). % Action
find(Connection, Coll, Selector, Projector, Skip, BatchSize) ->
    mc_action_man:read(Connection, #'query'{
        collection = Coll,
        selector = Selector,
        projector = Projector,
        skip = Skip,
        batchsize = BatchSize
    }).
===============

对评论的答复:

在mc_action_man.erl文件中,它仍然使用光标保存“当前位置”

在mc_worker.erl中,它是实际发送到数据库的数据,我认为您可以添加write_log(例如:lager)代码来监视实际请求以发现问题

handle_call(Request, From, State = #state{socket = Socket, ets = Ets, conn_state = CS}) % read requests
    when is_record(Request, 'query'); is_record(Request, getmore) ->
    UpdReq = case is_record(Request, 'query') of
                 true -> Request#'query'{slaveok = CS#conn_state.read_mode =:= slave_ok};
                 false -> Request
             end,
    {ok, Id} = mc_worker_logic:make_request(Socket, CS#conn_state.database, UpdReq),
    inet:setopts(Socket, [{active, once}]),
    RespFun = fun(Response) -> gen_server:reply(From, Response) end,  % save function, which will be called on response
    true = ets:insert_new(Ets, {Id, RespFun}),
    {noreply, State};

您肯定是对的,虽然它似乎只批量处理游标上的请求数,但一旦游标上达到该数,它可能会获取下一批。。。但这是我真正需要的答案。感谢经过一些调查,似乎限制和批量大小显然不是一回事,当然。。。这是因为MongoDB Erlang驱动程序已过时或不完整吗?此外,请检查您的本地源代码版本,并从github进行更新。如果您希望更新答案以保持被接受的答案:
omongo:find(“collection”,{'$query',{value,},},$orderby',{'date,-1},$maxScan',5})
此命令使用
'$orderby'
进行排序,并使用
'$maxScan'
选项进行限制。我将在这两天内构造一些数据并给您回复,今天是中国春节。
handle_call(Request, From, State = #state{socket = Socket, ets = Ets, conn_state = CS}) % read requests
    when is_record(Request, 'query'); is_record(Request, getmore) ->
    UpdReq = case is_record(Request, 'query') of
                 true -> Request#'query'{slaveok = CS#conn_state.read_mode =:= slave_ok};
                 false -> Request
             end,
    {ok, Id} = mc_worker_logic:make_request(Socket, CS#conn_state.database, UpdReq),
    inet:setopts(Socket, [{active, once}]),
    RespFun = fun(Response) -> gen_server:reply(From, Response) end,  % save function, which will be called on response
    true = ets:insert_new(Ets, {Id, RespFun}),
    {noreply, State};