Skip to content

Commit

Permalink
feat: Supports passing arrayFilters option through find_one_and_update (
Browse files Browse the repository at this point in the history
  • Loading branch information
ksmithut authored Aug 30, 2023
1 parent e6eff31 commit 082878b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/mongo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,8 @@ defmodule Mongo do
writeConcern: write_concern(opts),
maxTimeMS: opts[:max_time],
collation: opts[:collation],
comment: opts[:comment]
comment: opts[:comment],
arrayFilters: opts[:array_filters]
]
|> filter_nils()

Expand Down
5 changes: 5 additions & 0 deletions test/mongo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ defmodule Mongo.Test do
assert {:ok, value} = Mongo.find_one_and_update(c.pid, coll, %{"foo" => 43}, %{"$set" => %{baz: 1}}, upsert: true, return_document: :after)
assert %{"foo" => 43, "baz" => 1} = value, "Should upsert"

# array_filters
assert {:ok, _} = Mongo.insert_one(c.pid, coll, %{foo: 44, things: [%{id: "123", name: "test"}, %{id: "456", name: "not test"}]})
assert {:ok, value} = Mongo.find_one_and_update(c.pid, coll, %{"foo" => 44}, %{"$set" => %{"things.$[sub].name" => "new"}}, array_filters: [%{"sub.id" => "123"}], return_document: :after)
assert %{"foo" => 44, "things" => [%{"id" => "123", "name" => "new"}, %{"id" => "456", "name" => "not test"}]} = value, "Should leverage array filters"

# don't find return {:ok, nil}
assert {:ok, nil} == Mongo.find_one_and_update(c.pid, coll, %{"number" => 666}, %{"$set" => %{title: "the number of the beast"}})

Expand Down

0 comments on commit 082878b

Please sign in to comment.