- 
    Type:Task 
- 
    Resolution: Works as Designed
- 
    Priority:Major - P3 
- 
    None
- 
    Affects Version/s: None
- 
    Component/s: None
- 
        Query Optimization
- 
        None
- 
        None
- 
        None
- 
        None
- 
        None
- 
        None
- 
        None
Context
Given the following code:
db.t.t.insertOne({"title": "test"}) db.t.t.aggregate([{"$lookup": {"as": "__subquery0", "from": "pepe", "let":
{"parent__field__0": "$field"}
, "pipeline": [{"$match": {"$expr":
{"$literal": false}
}}, {"$match": {"$expr": {"$and": [
{"$setIsSubset": ["$field", "$$parent__field__0"]}
]}}}]}}])
 
raises:
MongoServerError[Location17311]: PlanExecutor error during aggregation :: caused by :: Failed to optimize pipeline :: caused by :: Failed to optimize expression :: caused by :: Failed to optimize expression :: caused by :: both operands of $setIsSubset must be arrays. Second argument is of type: missing
 
Note: the lookup subquery has a {$match: {$expr: {$literal: false }}} to get an empty result.
and If a setIsSubset is used in a empty result but not inside a $lookup:
db.t.t.aggregate([{"$match": {"$expr":
{"$literal": false}
}}, {"$project": {expr:
{'$setIsSubset': ['$field', '$something']}
}}])
 
It didn't raise an error, just return an empty result.Note2: it seems to have something to do with the parent_fields, if the subquery does not use parent_field_0, it works:
db.t.t.aggregate([{"$lookup": {"as": "__subquery0", "from": "pepe", "let":
{"parent__field__0": "$field"}
, "pipeline": [{"$match": {"$expr":
{"$literal": false}
}}, {"$match": {"$expr": {"$and": [
{"$setIsSubset": ["$field", "$aa"]}
]}}}]}}])
 
Mongo version is:
Using MongoDB: 7.0.16
Using Mongosh: 2.3.0
-----------------
Additional attempts
Example using an undefined fieldname (`$aa`)
(Attached as image.png)
Example of a failed query using the $$parent_field_0
(Attached as image (1).png)

