0

I have to query a mongoid array for value combinations inside the array.

The query array example looks like this:

qa = [["a","b","c"], ["a","b"],["b","c"]]
Model.any_in(:field => qa)

What I need in return is all models in which the :field contains the values(but maybe also other ones) For example it should return Models where :field is ["b",'c','d'] too because of ["b","c"] in the query.

ATM my solution is to iterate over the query array and doing any_in for every nested array, then combining results. This worked of course, but now if have the situation of a vastly increased query array, like about 20k "subarrays". This takes way too much time. So far i tried combinations of any_in and any_of, all which only led me to a solution where I can query an exact match of the nested arrays, like models with ["a","b","c"] in the :field

tadman
  • 208,517
  • 23
  • 234
  • 262
RunsnbunsN
  • 88
  • 1
  • 11
  • For blocks of code, just indent with four spaces or use the `{}` button. – tadman Dec 11 '17 at 16:08
  • 1
    I don't know MongoID, but MongoDB has a [$setIsSubset](https://docs.mongodb.com/v3.4/reference/operator/aggregation/setIsSubset/) aggregation operator that can evidently be used in queries, does that help? – Mark Thomas Dec 11 '17 at 16:37
  • 1
    According to [this SO question](https://stackoverflow.com/questions/12223465/mongodb-query-subset-of-an-array), `$all` behaves like a subset query. – Mark Thomas Dec 11 '17 at 16:42
  • I tried `$all` which basically maps to just `.all` in mongoid and doesnt work unfortunately. `$setIsSubset` looks interesting but isnt implemented in Mongoid, I guess a "raw" query would work, maybe I can stitch one together. – RunsnbunsN Dec 11 '17 at 18:58

0 Answers0