My MongoDB document schema looks like this:
[
{
"id": 1,
"data": [
{
"val1": "xyz",
"val2": "abc"
},
{
"val1": "a",
"val2": "b"
}
]
},
{
"id": 2,
"data": [
{
"val1": "d",
"val2": "e"
},
{
"val1": "f",
"val2": "f"
}
]
},
]
I need to find documents with data.0.val1="xyz"
and data.0.val2="abc"
and following is my query which works fine:
db.collection.find({
"$and": [
{
"data.0.val1": {
$eq: "xyz"
}
},
{
"data.0.val2": {
$eq: "abc"
}
}
]
})
But when I tried using $eq aggregation operator along with array dot notation, it is not returning expected results:
db.collection.find({
$expr: {
$and: [
{
$eq: [
"$data.0.val1",
"xyz"
]
},
{
$eq: [
"$data.0.val2",
"abc"
]
}
]
}
})
Does $eq aggregation operator works with array dot notation in find() ? What am I doing wrong?