Skip to content

Commit 4c817b7

Browse files
committed
fix: add support for the hint option (#175)
1 parent 6e0c8b2 commit 4c817b7

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

lib/mongo.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,8 @@ defmodule Mongo do
11091109
upsert: Keyword.get(opts, :upsert),
11101110
multi: multi,
11111111
collation: Keyword.get(opts, :collation),
1112-
arrayFilters: Keyword.get(opts, :array_filters)
1112+
arrayFilters: Keyword.get(opts, :array_filters),
1113+
hint: Keyword.get(opts, :hint)
11131114
}
11141115
|> filter_nils()
11151116

test/mongo/update_hint_test.exs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
defmodule Mongo.UpdateHintTest do
2+
use CollectionCase
3+
4+
test "update_one, using :hint options", %{pid: top} do
5+
coll = unique_collection()
6+
7+
Mongo.insert_one(top, coll, %{_id: 1, member: "abc123", status: "P", points: 0, misc1: nil, misc2: nil})
8+
Mongo.insert_one(top, coll, %{_id: 2, member: "xyz123", status: "A", points: 60, misc1: "reminder: ping me at 100pts", misc2: "Some random comment"})
9+
Mongo.insert_one(top, coll, %{_id: 3, member: "lmn123", status: "P", points: 0, misc1: nil, misc2: nil})
10+
Mongo.insert_one(top, coll, %{_id: 4, member: "pqr123", status: "D", points: 20, misc1: "Deactivated", misc2: nil})
11+
Mongo.insert_one(top, coll, %{_id: 5, member: "ijk123", status: "P", points: 0, misc1: nil, misc2: nil})
12+
Mongo.insert_one(top, coll, %{_id: 6, member: "cde123", status: "A", points: 86, misc1: "reminder: ping me at 100pts", misc2: "Some random comment"})
13+
14+
assert :ok = Mongo.create_indexes(top, coll, [%{key: %{status: 1}, name: "status_index"}, %{key: %{points: 1}, name: "points_index"}])
15+
16+
assert {:ok,
17+
%Mongo.UpdateResult{
18+
acknowledged: true,
19+
matched_count: 3,
20+
modified_count: 3,
21+
upserted_ids: []
22+
}} = Mongo.update_many(top, coll, %{points: %{"$lte": 20}, status: "P"}, %{"$set": %{misc1: "Need to activate"}}, hint: %{status: 1})
23+
24+
assert {:error, %{write_errors: [%{"code" => 2, "index" => 0}]}} = Mongo.update_many(top, coll, %{points: %{"$lte": 20}, status: "P"}, %{"$set": %{misc1: "Need to activate"}}, hint: %{email: 1})
25+
end
26+
end

0 commit comments

Comments
 (0)