Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .dialyzer-ignore.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
{":0:unknown_type Unknown type: Mongo.WriteError.t/0." }
]
27 changes: 16 additions & 11 deletions lib/mongo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ defmodule Mongo do
* `:max_time` - Specifies a time limit in milliseconds
* `:use_cursor` - Use a cursor for a batched response (Default: true)
"""
@spec aggregate(GenServer.server(), collection, [BSON.document()], Keyword.t()) :: cursor
@spec aggregate(GenServer.server(), collection, [BSON.document()], Keyword.t()) ::
cursor | {:error, Mongo.Error.t()}
def aggregate(topology_pid, coll, pipeline, opts \\ []) do
query =
[
Expand Down Expand Up @@ -482,7 +483,8 @@ defmodule Mongo do
* `:projection` - Limits the fields to return for all matching document
* `:skip` - The number of documents to skip before returning (Default: 0)
"""
@spec find(GenServer.server(), collection, BSON.document(), Keyword.t()) :: cursor
@spec find(GenServer.server(), collection, BSON.document(), Keyword.t()) ::
cursor | {:error, Mongo.Error.t()}
def find(topology_pid, coll, filter, opts \\ []) do
query =
[
Expand Down Expand Up @@ -538,7 +540,7 @@ defmodule Mongo do
* `:skip` - The number of documents to skip before returning (Default: 0)
"""
@spec find_one(GenServer.server(), collection, BSON.document(), Keyword.t()) ::
BSON.document() | nil
BSON.document() | nil | {:error, Mongo.Error.t()}
def find_one(conn, coll, filter, opts \\ []) do
opts =
opts
Expand Down Expand Up @@ -597,10 +599,10 @@ defmodule Mongo do
rp = ReadPreference.defaults(%{mode: :primary})
rp_opts = [read_preference: Keyword.get(opts, :read_preference, rp)]

with {:ok, conn, slave_ok, _} <- select_server(topology_pid, :read, rp_opts) do
opts = Keyword.put(opts, :slave_ok, slave_ok)
direct_command(conn, query, opts)
end
with {:ok, conn, slave_ok, _} <- select_server(topology_pid, :read, rp_opts) do
opts = Keyword.put(opts, :slave_ok, slave_ok)
direct_command(conn, query, opts)
end
end

@doc false
Expand Down Expand Up @@ -1001,7 +1003,8 @@ defmodule Mongo do
@doc """
Returns a cursor to enumerate all indexes
"""
@spec list_indexes(GenServer.server(), String.t(), Keyword.t()) :: cursor
@spec list_indexes(GenServer.server(), String.t(), Keyword.t()) ::
cursor | {:error, Mongo.Error.t()}
def list_indexes(topology_pid, coll, opts \\ []) do
with {:ok, conn, _, _} <- select_server(topology_pid, :read, opts) do
aggregation_cursor(conn, "$cmd", [listIndexes: coll], nil, opts)
Expand All @@ -1011,7 +1014,8 @@ defmodule Mongo do
@doc """
Convenient function that returns a cursor with the names of the indexes.
"""
@spec list_index_names(GenServer.server(), String.t(), Keyword.t()) :: %Stream{}
@spec list_index_names(GenServer.server(), String.t(), Keyword.t()) ::
%Stream{} | {:error, Mongo.Error.t()}
def list_index_names(topology_pid, coll, opts \\ []) do
list_indexes(topology_pid, coll, opts)
|> Stream.map(fn %{"name" => name} -> name end)
Expand All @@ -1020,7 +1024,7 @@ defmodule Mongo do
@doc """
Getting Collection Names
"""
@spec show_collections(GenServer.server(), Keyword.t()) :: cursor
@spec show_collections(GenServer.server(), Keyword.t()) :: cursor | {:error, Mongo.Error.t()}
def show_collections(topology_pid, opts \\ []) do
##
# from the specs
Expand Down Expand Up @@ -1095,7 +1099,8 @@ defmodule Mongo do
select_servers(topology_pid, type, opts, start_time)

{:error, :selection_timeout} ->
{:error, %Mongo.Error{type: :network, message: "Topology selection timeout", code: 89}}
{:error,
%Mongo.Error{type: :network, message: "Topology selection timeout", code: 89}}
end
else
{:ok, servers, slave_ok, mongos?}
Expand Down
7 changes: 4 additions & 3 deletions lib/mongo/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,10 @@ defmodule Mongo.Session do
@impl :gen_statem
# Abort all pending transactions if there any and end session itself.
def terminate(_reason, state, %{pid: pid} = data) do
if state == :in_transaction do
_ = try_run_txn_command(data, :abortTransaction)
end
_ =
if state == :in_transaction do
try_run_txn_command(data, :abortTransaction)
end

query = %{
endSessions: [data.id]
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ defmodule Mongodb.Mixfile do
description: description(),
package: package(),
dialyzer: [
ignore_warnings: ".dialyzer-ignore.exs",
flags: [:underspecs, :unknown, :unmatched_returns],
plt_add_apps: [:logger, :connection, :db_connection, :mix, :elixir, :ssl, :public_key],
plt_add_deps: :transitive
Expand Down