mongodb_ecto icon indicating copy to clipboard operation
mongodb_ecto copied to clipboard

Where in array + dynamic limit and offset

Open exAspArk opened this issue 6 years ago • 1 comments
trafficstars

Hey!

I have a weird bug in my production application which I was able to reproduce in the repo with the latest master in test/mongo_ecto_test.exs:

  test "where in ids + dynamic limit + dynamic offset" do
    post1 = TestRepo.insert!(%Post{})
    post2 = TestRepo.insert!(%Post{})
    ids = [post1.id, post2.id]
    limit = 1
    offset = 1

    query = from p in Post, where: p.id in ^ids, limit: ^limit, offset: ^offset
    assert TestRepo.all(query) == [post2]
  end
  • When I try to use p.id in ^ids with limit: ^limit, it fails with the following error (the id from ids is somehow used as a count):
     ** (ArithmeticError) bad argument in arithmetic expression
     code: assert TestRepo.all(query) == [post2]
     stacktrace:
       :erlang.-(#BSON.ObjectId<5c40b5cdac013d9153aefc9f>, 1)
       (mongodb) lib/mongo/cursor.ex:106: Enumerable.Mongo.Cursor.new_limit/2
       (mongodb) lib/mongo/cursor.ex:44: anonymous fn/6 in Enumerable.Mongo.Cursor.start_fun/6
       (elixir) lib/stream.ex:1362: anonymous fn/5 in Stream.resource/3
       (elixir) lib/enum.ex:2979: Enum.map_reduce/3
       (mongodb_ecto) lib/mongo_ecto.ex:600: Mongo.Ecto.execute/6
       (ecto) lib/ecto/repo/queryable.ex:130: Ecto.Repo.Queryable.execute/5
       (ecto) lib/ecto/repo/queryable.ex:35: Ecto.Repo.Queryable.all/4
       test/mongo_ecto_test.exs:104: (test)
  • When I try to use p.id in ^ids with offset: ^offset, it fails with another error:
12:05:34.821 [error] GenServer #PID<0.295.0> terminating
** (DBConnection.ConnectionError) client #PID<0.309.0> stopped: ** (ArgumentError) argument error
    (mongodb) lib/mongo/messages.ex:116: Mongo.Messages.encode_op/1
    (mongodb) lib/mongo/messages.ex:59: Mongo.Messages.encode/2
    (mongodb) lib/mongo/protocol/utils.ex:32: Mongo.Protocol.Utils.send/3
    (mongodb) lib/mongo/protocol/utils.ex:12: Mongo.Protocol.Utils.message/3
    (mongodb) lib/mongo/protocol.ex:273: Mongo.Protocol.message_reply/2
    (mongodb) lib/mongo/protocol.ex:186: Mongo.Protocol.handle_execute/4
    (db_connection) lib/db_connection.ex:958: DBConnection.handle/4
    (db_connection) lib/db_connection.ex:1100: anonymous fn/4 in DBConnection.run_execute/4
    (db_connection) lib/db_connection.ex:1142: anonymous fn/4 in DBConnection.run_meter/5
    (db_connection) lib/db_connection.ex:1199: DBConnection.run_begin/3
    (db_connection) lib/db_connection.ex:636: DBConnection.execute/4
    (mongodb) lib/mongo.ex:414: Mongo.raw_find/5
    (mongodb) lib/mongo/cursor.ex:40: anonymous fn/6 in Enumerable.Mongo.Cursor.start_fun/6
    (elixir) lib/stream.ex:1362: anonymous fn/5 in Stream.resource/3
    (elixir) lib/enum.ex:2979: Enum.map_reduce/3
    (mongodb_ecto) lib/mongo_ecto.ex:600: Mongo.Ecto.execute/6
    (ecto) lib/ecto/repo/queryable.ex:130: Ecto.Repo.Queryable.execute/5
    (ecto) lib/ecto/repo/queryable.ex:35: Ecto.Repo.Queryable.all/4
    test/mongo_ecto_test.exs:105: Mongo.EctoTest."test where in + limit"/1
    (ex_unit) lib/ex_unit/runner.ex:312: ExUnit.Runner.exec_test/1
    (db_connection) lib/db_connection/connection.ex:243: DBConnection.Connection.handle_cast/2
    (connection) lib/connection.ex:810: Connection.handle_async/3
    (stdlib) gen_server.erl:637: :gen_server.try_dispatch/4
    (stdlib) gen_server.erl:711: :gen_server.handle_msg/6
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

exAspArk avatar Jan 17 '19 17:01 exAspArk

Opened this PR to address the issue https://github.com/ankhers/mongodb_ecto/pull/171.

exAspArk avatar Jan 17 '19 22:01 exAspArk