-
Notifications
You must be signed in to change notification settings - Fork 64
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1129,7 +1129,29 @@ defmodule Mongo do | |
|
||
e.g. long-hand `query` becomes short-hand `q`, snake case `array_filters` | ||
becomes `arrayFilters` | ||
|
||
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] | ||
]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(%{ | ||
|
@@ -1170,11 +1192,12 @@ defmodule Mongo do | |
end | ||
|
||
defp normalise_updates([[{_, _} | _] | _] = updates) do | ||
updates | ||
|> Enum.map(&normalise_update/1) | ||
Enum.map(updates, &normalise_update/1) | ||
end | ||
|
||
defp normalise_updates(updates), do: normalise_updates([updates]) | ||
defp normalise_updates([{_, _} | _] = updates), do: normalise_updates([updates]) | ||
|
||
defp normalise_updates(updates), do: updates | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
it would be nice to have
|
||
|
||
defp normalise_update(update) do | ||
update | ||
|
There was a problem hiding this comment.
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.