Skip to content
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
32 changes: 32 additions & 0 deletions src/mango/src/mango_selector.erl
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ norm_ops({[{<<"$allMatch">>, {_}=Arg}]}) ->
norm_ops({[{<<"$allMatch">>, Arg}]}) ->
?MANGO_ERROR({bad_arg, '$allMatch', Arg});

norm_ops({[{<<"$keyMapMatch">>, {_}=Arg}]}) ->
{[{<<"$keyMapMatch">>, norm_ops(Arg)}]};
norm_ops({[{<<"$keyMapMatch">>, Arg}]}) ->
?MANGO_ERROR({bad_arg, '$keyMapMatch', Arg});

norm_ops({[{<<"$size">>, Arg}]}) when is_integer(Arg), Arg >= 0 ->
{[{<<"$size">>, Arg}]};
norm_ops({[{<<"$size">>, Arg}]}) ->
Expand Down Expand Up @@ -253,6 +258,10 @@ norm_fields({[{<<"$allMatch">>, Arg}]}, Path) ->
Cond = {[{<<"$allMatch">>, norm_fields(Arg)}]},
{[{Path, Cond}]};

norm_fields({[{<<"$keyMapMatch">>, Arg}]}, Path) ->
Cond = {[{<<"$keyMapMatch">>, norm_fields(Arg)}]},
{[{Path, Cond}]};


% The text operator operates against the internal
% $default field. This also asserts that the $default
Expand Down Expand Up @@ -334,6 +343,9 @@ norm_negations({[{<<"$elemMatch">>, Arg}]}) ->
norm_negations({[{<<"$allMatch">>, Arg}]}) ->
{[{<<"$allMatch">>, norm_negations(Arg)}]};

norm_negations({[{<<"$keyMapMatch">>, Arg}]}) ->
{[{<<"$keyMapMatch">>, norm_negations(Arg)}]};

% All other conditions can't introduce negations anywhere
% further down the operator tree.
norm_negations(Cond) ->
Expand Down Expand Up @@ -491,6 +503,26 @@ match({[{<<"$allMatch">>, Arg}]}, [_ | _] = Values, Cmp) ->
match({[{<<"$allMatch">>, _Arg}]}, _Value, _Cmp) ->
false;

% Matches when any key in the map value matches the
% sub-selector Arg.
match({[{<<"$keyMapMatch">>, Arg}]}, Value, Cmp) when is_tuple(Value) ->
try
lists:foreach(fun(V) ->
case match(Arg, V, Cmp) of
true -> throw(matched);
_ -> ok
end
end, [Key || {Key, _} <- element(1, Value)]),
false
catch
throw:matched ->
true;
_:_ ->
false
end;
match({[{<<"$keyMapMatch">>, _Arg}]}, _Value, _Cmp) ->
false;

% Our comparison operators are fairly straight forward
match({[{<<"$lt">>, Arg}]}, Value, Cmp) ->
Cmp(Value, Arg) < 0;
Expand Down
9 changes: 9 additions & 0 deletions src/mango/test/03-operator-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ def test_empty_all_match(self):
docs = self.db.find({"emptybang": {"$allMatch": {"foo": {"$eq": 2}}}})
self.assertEqual(len(docs), 0)

def test_keymap_match(self):
amdocs = [
{"foo": {"aa": "bar", "bb": "bang"}},
{"foo": {"cc": "bar", "bb": "bang"}},
]
self.db.save_docs(amdocs, w=3)
docs = self.db.find({"foo": {"$keyMapMatch": {"$eq": "aa"}}})
self.assertEqual(len(docs), 1)

def test_in_operator_array(self):
docs = self.db.find({"manager": True, "favorites": {"$in": ["Ruby", "Python"]}})
self.assertUserIds([2, 6, 7, 9, 11, 12], docs)
Expand Down