I have this simple test document
[
{
arry: [0,1],
d: 1
},
{
arry: [1,0],
d:0
}
]
I’d like to compare one array element to another. I tried using the $expr with dot notation and it didn’t work
db.col.find({$expr: {$gt: [‘$arry.0’, ‘$arry.1’]}})
Then I tried this and it worked
db.col.aggregate({$project: {all: "$$ROOT, a0: {$slice: [‘$arry’, 0, 1]}, a1: {$slice: [‘$arry’, 1,1]}}}, {$match: {$expr: {$lt: [‘$a0’, ‘$a1’]}}}])
But this failed and I don’t know why
db.col.aggregate({$project: {all: "$$ROOT, a0: {$slice: [‘$arry’, 0, 1]}, a1: {$slice: [‘$arry’, 1,1]}}}, {$match: {$expr: {$lt: [‘$a0’, ‘$all.d’]}}}])
I also tried using $arrayElemAt to extract the array elements but same result.
Please tell me what I’m doing wrong here. Thank you!