Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(update/4): improve Mongo.update/4 function #245

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
34 changes: 30 additions & 4 deletions lib/mongo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,29 @@ defmodule Mongo do

e.g. long-hand `query` becomes short-hand `q`, snake case `array_filters`
becomes `arrayFilters`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make a mention of some of the usual options (upsert & multi) ? I was a bit surprised they couldn't be combined, but it makes sense after some thought.

Example:

Mongo.update(MongoPool,
"test_collection",
query: %{foo => 4},
update: %{"$set": %{"modified_field": "new_value"}},
multi: true)

Mongo.update(MongoPool,
"test_collection",
query: %{foo: 4},
update: %{foo: 5, new_field: "new_value"}},
upsert: true)

Mongo.update(MongoPool, "test_collection", [
[q: %{foo: 24}, update: %{flag: "old"}],
[q: %{foo: 99}, update: %{luftballons: "yes"}, upsert: true]
])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these examples suffice? I think this shows the most common ways of supplying updates, WDYT?

"""
@spec update(GenServer.server(), collection, [Keyword.t()], Keyword.t()) :: result(Mongo.UpdateResult.t())
def update(topology_pid, coll, updates, opts \\ [])

def update(topology_pid, coll, updates, opts) do
write_concern =
filter_nils(%{
Expand Down Expand Up @@ -1169,12 +1191,16 @@ defmodule Mongo do
end
end

defp normalise_updates([[{_, _} | _] | _] = updates) do
updates
|> Enum.map(&normalise_update/1)
# maps list of updates (which are Keyword lists) to Mongo updates
defp normalise_updates([[{_key, _value} | _rest] | _updates] = updates) do
Enum.map(updates, &normalise_update/1)
end

defp normalise_updates(updates), do: normalise_updates([updates])
# maps a single update (= Keyword list) to Mongo update
defp normalise_updates([{_key, _value} | _rest] = updates), do: normalise_updates([updates])

# let Mongo evaluate if this is correct input
defp normalise_updates(updates), do: updates
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only normalise Keyword lists, letting Mongo return an error if the updates are not in an expected format

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some names for the placeholders to increase the readability? For example:

Instead of

defp normalise_updates([[{_, _} | _] | _] = updates) do

it would be nice to have

## maps lists of keywords list
defp normalise_updates([[{_key, _value} | _rest] | _updates] = updates) do


defp normalise_update(update) do
update
Expand Down
18 changes: 18 additions & 0 deletions test/mongo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,24 @@ defmodule Mongo.Test do
end
end

test "update", c do
coll = unique_name()

assert {:ok, _} = Mongo.insert_many(c.pid, coll, [%{foo: 42}, %{foo: 42}, %{_id: 1}])

assert {:ok, %Mongo.UpdateResult{acknowledged: true, matched_count: 2, modified_count: 2, upserted_ids: []}} = Mongo.update(c.pid, coll, q: %{foo: 42}, update: %{"$set": %{foo: 0}}, multi: true)

assert {:ok, %Mongo.UpdateResult{acknowledged: true, matched_count: 0, modified_count: 0, upserted_ids: []}} == Mongo.update(c.pid, coll, [query: %{foo: 0}, update: %{}], w: 0)

assert {:ok, %Mongo.UpdateResult{acknowledged: true, matched_count: 1, modified_count: 0, upserted_ids: [%BSON.ObjectId{}]}} = Mongo.update(c.pid, coll, query: %{foo: 100}, update: %{foo: 24, flag: "new"}, upsert: true)

assert {:ok, %Mongo.UpdateResult{acknowledged: true, matched_count: 2, modified_count: 1, upserted_ids: [%BSON.ObjectId{}]}} =
Mongo.update(c.pid, coll, [[q: %{foo: 24}, update: %{flag: "old"}], [q: %{foo: 99}, update: %{luftballons: "yes"}, upsert: true]])

# message: "Write batch sizes must be between 1 and 100000. Got 0 operations."
assert {:error, %Mongo.Error{code: 16}} = Mongo.update(c.pid, coll, [])
end

# issue #19
# test "correctly pass options to cursor", c do
# assert %Mongo.AggregationCursor{opts: [slave_ok: true, no_cursor_timeout: true], coll: "coll"} =
Expand Down
Loading