I have mongodb document it has key which is an array. i want to check that the 2nd element in array exists or not. if exists then return the documents otherwise return no document.
example document :
{
_id : "672d75267",
name : "somename",
arr : [1,2,3]
}
db.collection.aggregate([
{
$match : {
$expr : {
$arrayElemAt : ["$arr",2]
}
}
}
])
>Solution :
Array index start from 0 that is why we have taken "arr.1"
db.collection.aggregate([
{
$match: {
"arr.1": {
"$exists": true
}
}
},
])