Find Documents in MongoDB whose with an array field is a subset of a query array
mongodb
Solution
In MongoDb, for array field:
"$in:[...]" means "intersection" or "any element in",
"$all:[...]" means "subset" or "contain",
"$elemMatch:{...}" means "any element match"
"$not:{$elemMatch:{$nin:[...]}}" means "superset" or "in"
Problem
Suppose I have a insert a set of documents each with an `array` field. I would like to find all documents such that their `array` field is a subset of a query array. For example, if I have the following documents, ``` collection.insert([ { 'name': 'one', 'array': ['a', 'b', 'c'] }, { 'name': 'two', 'array': ['b', 'c', 'd'] }, { 'name': 'three', 'array': ['b', 'c'] } ]) ``` and I query `collection.find({'array': {'$superset': ['a', 'b', 'c']})`, I would expect to see documents `one` and `three` as `['a', 'b', 'c']` and `['b', 'c']` are both subsets of `['a', 'b', 'c']`. In other words, I'd like to do the inverse of Mongo's `$all` query, which selects all documents such that the query array is a subset of the document's `array` field. Is this possible? and if so, how?