Skip to content

Commit 082878b

Browse files
authored
feat: Supports passing arrayFilters option through find_one_and_update (#201)
1 parent e6eff31 commit 082878b

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

lib/mongo.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,8 @@ defmodule Mongo do
491491
writeConcern: write_concern(opts),
492492
maxTimeMS: opts[:max_time],
493493
collation: opts[:collation],
494-
comment: opts[:comment]
494+
comment: opts[:comment],
495+
arrayFilters: opts[:array_filters]
495496
]
496497
|> filter_nils()
497498

test/mongo_test.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ defmodule Mongo.Test do
248248
assert {:ok, value} = Mongo.find_one_and_update(c.pid, coll, %{"foo" => 43}, %{"$set" => %{baz: 1}}, upsert: true, return_document: :after)
249249
assert %{"foo" => 43, "baz" => 1} = value, "Should upsert"
250250

251+
# array_filters
252+
assert {:ok, _} = Mongo.insert_one(c.pid, coll, %{foo: 44, things: [%{id: "123", name: "test"}, %{id: "456", name: "not test"}]})
253+
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)
254+
assert %{"foo" => 44, "things" => [%{"id" => "123", "name" => "new"}, %{"id" => "456", "name" => "not test"}]} = value, "Should leverage array filters"
255+
251256
# don't find return {:ok, nil}
252257
assert {:ok, nil} == Mongo.find_one_and_update(c.pid, coll, %{"number" => 666}, %{"$set" => %{title: "the number of the beast"}})
253258

0 commit comments

Comments
 (0)