MongoDB Erlang Driver find limit 出现在哪里?

MongoDB Erlang Driver find limit occurs where?

我正在尝试在 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)

我认为您可以使用 batch_size 参数。 以下代码来自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文件中,仍然使用游标保存"current postion"。

read(Connection, Request = #'query'{collection = Collection, batchsize = BatchSize} ) ->
    {Cursor, Batch} = mc_connection_man:request(Connection, Request),
    mc_cursor:create(Connection, Collection, Cursor, BatchSize, Batch).

在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};