Q:

Mongoose array elements ALL $in array

I have an array like this:
{
    "_id" : ObjectId("581b7d650949a5204e0a6e9b"),
    "types" : [ 
        {
            "type" : ObjectId("581b7c645057c4602f48627f"),
            "quantity" : 4,
            "_id" : ObjectId("581b7d650949a5204e0a6e9e")
        }, 
        {
            "type" : ObjectId("581ca0e75b1e3058521a6d8c"),
            "quantity" : 4,
            "_id" : ObjectId("581b7d650949a5204e0a6e9e")
        }
    ],
    "__v" : 0
},
{
    "_id" : ObjectId("581b7d650949a5204e0a6e9c"),
    "types" : [ 
        {
            "type" : ObjectId("581b7c645057c4602f48627f"),
            "quantity" : 4,
            "_id" : ObjectId("581b7d650949a5204e0a6e9e")
        }
    ],
    "__v" : 0
}

And I want to create a query that will return me the elementswhere the array of types ALL match a $in array.
For example:
query([ObjectId("581b7c645057c4602f48627f"), ObjectId("581ca0e75b1e3058521a6d8c")])

should return elements 1 and 2
query([ObjectId("581b7c645057c4602f48627f")])

should return element 2
query([ObjectId("581ca0e75b1e3058521a6d8c")])

should return nothing
I tried
db.getCollection('elements').find({'types.type': { $in: [ObjectId("581ca0e75b1e3058521a6d8c")]}})

But it returns the elements if only one types matches

A:

You may have to use aggregation as $in and $elematch will return only matching elements. Project stage does set equals to create a all match flag and matches in the last stage with true value.
aggregate([ {
    $project: {
        _id: 0,
        isAllMatch: {$setIsSubset: ["$types.type", [ObjectId("581b7c645057c4602f48627f")]]},
        data: "$$ROOT"
    }
}, {
    $match: {
        isAllMatch: true
    }
}])

Sample Output
{
    "isAllMatch": true,
    "data": {
        "_id": ObjectId("581b7d650949a5204e0a6e9c"),
        "types": [{
            "type": ObjectId("581b7c645057c4602f48627f"),
            "quantity": 4,
            "_id": ObjectId("581b7d650949a5204e0a6e9e")
        }],
        "__v": 0
    }
}

Alternative version: 
This version combines both project and match stages into one $redact stage with $cond operator to decide whether to keep or prune the elements.
aggregate([{
    "$redact": {
        "$cond": [{
                $setIsSubset: ["$types.type", [ObjectId("581b7c645057c4602f48627f")]]
            },
            "$$KEEP",
            "$$PRUNE"
        ]
    }
}])

