I need to find documents in a collection matching a comparison between two dates, one of them being in an array in the document.
I found a solution using a combination of $arrayElemAt with aggregation pipeline but stand frustrated with the problem.
I originally thought the problem came from how I used the $lte operator with Date fields but I managed to narrow down the issue to a much simpler reproducible one.
db.getCollection('test-expr').insert({
"value1" : 1,
"value2" : 1,
"values" : [
1
]
})
Now trying to find the document with $expr:
db.getCollection('test-expr').find({$expr: {$eq: ["$value1", "$value2"]}})
=> Returns the document I just inserted.
Trying to compare the first element of the array using dot notation:
db.getCollection('test-expr').find({$expr: {$eq: ["$value1", "$values.0"]}})
=> Returns nothing.
I think maybe the dot notation doesn't work in $expr but I couldn't find anything pointing that out in the MongoDB documentation.
As mentioned before, I have found solutions to answer my problem but would like to understand why it cannot be achieved this way.
Am I missing something?
$in: docs.mongodb.com/manual/reference/operator/aggregation/in/…