How to write a mango filter on the size of an array?

237 Views Asked by At

I would like to find documents with a mango query by specifying the minimal and maximal size of an array property. Given a document with an array property customers. I'd like to be able to find all documents with the number of customers between 10 and 20.

Something like

mango_query = {
  "doc.customers": {"$size": {"gte": 10}},
  "doc.customers": {"$size": {"lte": 20}}
}

The response to a request like that is

Bad argument for operator $size: {[{<<36,108,116,101>>,10}]}')

So how should I write a mango filter on the size of an array?

1

There are 1 best solutions below

5
On

Checking the code here, only integer argument is supported for $size operator. So it can't be combined with other operators. It supports only $size exact matches.

norm_ops({[{<<"$size">>, Arg}]}) when is_integer(Arg), Arg >= 0 ->
    {[{<<"$size">>, Arg}]};
norm_ops({[{<<"$size">>, Arg}]}) ->
    ?MANGO_ERROR({bad_arg, '$size', Arg});

And when matching

match({[{<<"$size">>, Arg}]}, Values, _Cmp) when is_list(Values) ->
    length(Values) == Arg;
match({[{<<"$size">>, _}]}, _Value, _Cmp) ->
    false;

length(Values) == Arg Only exact match is supported