diff --git a/lib/mongo.ex b/lib/mongo.ex index d4bd1011..a40c5814 100644 --- a/lib/mongo.ex +++ b/lib/mongo.ex @@ -541,26 +541,36 @@ defmodule Mongo do @doc false def raw_find(conn, coll, query, select, opts) do - params = [query, select] - query = %Query{action: :find, extra: coll} - - with {:ok, _query, reply} <- DBConnection.execute(conn, query, params, defaults(opts)), - :ok <- maybe_failure(reply), - op_reply(docs: docs, cursor_id: cursor_id, from: from, num: num) = reply, - do: {:ok, %{from: from, num: num, cursor_id: cursor_id, docs: docs}} + query = filter_nils([ + find: coll, + filter: query, + projection: select, + batchSize: opts[:batch_size], + skip: opts[:skip], + ]) + + opts = Keyword.drop(opts, [:skip, :batch_size]) + + with {:ok, %{"cursor" => %{"id" => id, "firstBatch" => docs}}} <- + direct_command(conn, query, opts) do + {:ok, %{from: 0, num: Enum.count(docs), cursor_id: id, docs: docs}} + end end @doc false def get_more(conn, coll, cursor, opts) do - query = - filter_nils( - getMore: cursor, - collection: coll, - batchSize: Keyword.get(opts, :batch_size), - maxTimeMS: Keyword.get(opts, :max_time_ms) - ) + query = filter_nils([ + getMore: cursor, + collection: coll, + batchSize: opts[:batch_size], + ]) + + opts = Keyword.drop(opts, [:batch_size]) - direct_command(conn, query, opts) + with {:ok, %{"cursor" => %{"id" => id, "nextBatch" => docs}}} <- + direct_command(conn, query, opts) do + {:ok, %{from: 0, num: Enum.count(docs), cursor_id: id, docs: docs}} + end end @doc false diff --git a/lib/mongo/auth/scram.ex b/lib/mongo/auth/scram.ex index 3ad36a71..495a4204 100644 --- a/lib/mongo/auth/scram.ex +++ b/lib/mongo/auth/scram.ex @@ -34,7 +34,7 @@ defmodule Mongo.Auth.SCRAM do end defp first( - %{"conversationId" => 1, "payload" => server_payload, "done" => false}, + %{"conversationId" => conversation_id, "payload" => server_payload, "done" => false}, first_bare, username, password, @@ -54,18 +54,18 @@ defmodule Mongo.Auth.SCRAM do server_signature = generate_signature(salted_password, auth_message) proof = generate_proof(salted_password, auth_message) client_final_message = %BSON.Binary{binary: "#{client_message},#{proof}"} - message = [saslContinue: 1, conversationId: 1, payload: client_final_message] + message = [saslContinue: 1, conversationId: conversation_id, payload: client_final_message] {message, server_signature} end - defp second(%{"conversationId" => 1, "payload" => payload, "done" => false}, signature) do + defp second(%{"conversationId" => conversation_id, "payload" => payload}, signature) do params = parse_payload(payload) ^signature = Base.decode64!(params["v"]) - [saslContinue: 1, conversationId: 1, payload: %BSON.Binary{binary: ""}] + [saslContinue: 1, conversationId: conversation_id, payload: %BSON.Binary{binary: ""}] end - defp final(%{"conversationId" => 1, "payload" => %BSON.Binary{binary: ""}, "done" => true}) do + defp final(%{"conversationId" => _, "payload" => %BSON.Binary{binary: ""}, "done" => true}) do :ok end