Skip to content

Commit b868998

Browse files
committed
fix: improved the spec for function find_one_and_delete thanks to csrl
1 parent b64003c commit b868998

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

lib/mongo.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ defmodule Mongo do
599599
* `:collation` - Optionally specifies a collation to use in MongoDB 3.4 and higher.
600600
"""
601601
@spec find_one_and_delete(GenServer.server(), collection, BSON.document(), Keyword.t()) ::
602-
result(BSON.document())
602+
result(BSON.document()) | {:ok, nil}
603603
def find_one_and_delete(topology_pid, coll, filter, opts \\ []) do
604604
write_concern = write_concern(opts)
605605

test/mongo_test.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ defmodule Mongo.Test do
298298
assert {:ok, %Mongo.FindAndModifyResult{value: value}} = Mongo.find_one_and_replace(c.pid, coll, %{"upsertedDocument" => true}, %{"upsertedDocument" => true}, upsert: true, return_document: :after)
299299
assert %{"upsertedDocument" => true} = value, "Should upsert"
300300
assert [%{"upsertedDocument" => true}] = Mongo.find(c.pid, coll, %{upsertedDocument: true}) |> Enum.to_list()
301+
302+
assert {:ok, %Mongo.FindAndModifyResult{matched_count: 0, updated_existing: false, value: nil}} == Mongo.find_one_and_replace(c.pid, coll, %{"never" => "matching"}, %{})
303+
assert {:ok, %Mongo.FindAndModifyResult{matched_count: 0, updated_existing: false, value: nil}} == Mongo.find_one_and_replace(c.pid, "coll_that_doesnt_exist", %{"never" => "matching"}, %{})
301304
end
302305

303306
test "find_one_and_delete", c do
@@ -320,6 +323,8 @@ defmodule Mongo.Test do
320323

321324
assert {:ok, %{"note" => "delete"}} = Mongo.find_one_and_delete(c.pid, coll, %{foo: 50}, sort: %{bar: -1})
322325
assert [%{"note" => "keep"}] = Mongo.find(c.pid, coll, %{note: "keep"}) |> Enum.to_list()
326+
327+
assert {:ok, nil} = Mongo.find_one_and_delete(c.pid, coll, %{"never" => "matching"})
323328
end
324329

325330
test "insert_one", c do

0 commit comments

Comments
 (0)