Elixir 流式传输 mongodb 失败

Elixir streaming mondodb fail

我正在使用 elixir-mongo 并尝试流式传输查询结果。这是代码...

def archive_stream(z) do
Stream.resource(
  fn ->
      {jobs, datetime} = z
      lt = datetime_to_bson_utc datetime
      c = jobs |> Mongo.Collection.find( %{updated_at: %{"$lt": lt}}) |> Mongo.Find.exec
      {:cont, c.response.buffer, c}
  end,
  fn(z) ->
    {j, {cont, therest, c}} = next(z)
    case cont do
      :cont -> {j, {cont, therest, c}}
      :halt  -> {:halt, {cont, therest, c}}
    end
  end,
  fn(:halt, resp) -> resp end
)

结束

所有子位似乎都有效(如查询),但是当我尝试获取流时,我失败了...

Mdb.archive_stream({jobs, {{2013,11,1},{0,0,0}}})|>Enum.take(2)

我明白了...

(BadArityError) #Function<2.49475906/2 in Mdb.archive_stream/1> with arity 2 called with 1 argument ({:cont, <<90, 44, 0, 0, 7, 95, 105, 100, 0, 82, 110, 129, 221, 102, 160, 249, 201, 109, 0, 137, 233, 4, 95, 115, 108, 117, 103, 115, 0, 51, 0, 0, 0, 2, 48, 0, 39, 0, 0, 0, 109, 97, 110, 97, 103, 101, 114, 45, ...>>, %Mongo.Cursor{batchSize: 0, collection: %Mongo.Collection{db: %Mongo.Db{auth: {"admin", "edd5404c4f906060b125688e26ffb281"}, mongo: %Mongo.Server{host: 'db-stage.member0.mongolayer.com', id_prefix: 57602, mode: :passive, opts: %{}, port: 27017, socket: #Port<0.27099>, timeout: 6000}, name: "db-stage", opts: %{mode: :passive, timeout: 6000}}, name: "jobs", opts: %{}}, exhausted: false, response: %Mongo.Response{buffer: <<188, 14, 0, 0, 7, 95, 105, 100, 0, 82, 110, 129, 221, 102, 160, 249, 201, 109, 0, 137, 242, 4, 95, 115, 108, 117, 103, 115, 0, 45, 0, 0, 0, 2, 48, 0, 33, 0, 0, 0, 114, 101, 116, 97, ...>>, cursorID: 3958284337526732701, decoder: &Mongo.Response.bson_decode/1, nbdoc: 101, requestID: 67280413, startingFrom: 0}}}) (elixir) lib/stream.ex:1020: Stream.do_resource/5 (elixir) lib/enum.ex:1731: Enum.take/2

我很难过。有任何想法吗? 感谢帮助

该死!菜鸟错误。

:halt -> {:halt, {cont, therest, c}} 应该是 _ -> {:halt, z}fn(:halt, resp) -> resp end 应该是 fn(resp) -> resp end

一天半以来,除了 after 函数,我一直在研究其他所有东西。

给菜鸟们多一点解释...

  • next_fun() 中的最后一个选项可能 _ 以便捕获其他 "bad behavior" 而不仅仅是 {:halt}
  • after_fn() 只需要 1 个参数,在上面的代码中,next_fun() 最后一个选项中的 z 元组。不期望看到 :haltz,只是 z.

希望得到真正的专家的意见。