How can I query on the nth element of a subdocument array in MongoDB where n is variable?
Suppose, I've documents like bellow:
{
list:[
{
a: true,
b: 'abc'
},
{
a: false,
b: 'def'
},
{
a: true,
b: 'ghi'
},
]
}
Query 1: I need to find all documents which have
a: falseon the 1st element oflist(i.e.'list.0.a': false)Query 2: I need to find all documents which have
a: falseon the 2nd element oflist(i.e.'list.1.a': false)Query 3: I need to find all documents which have
a: falseon the 3rd element oflist(i.e.'list.2.a': false)
unwindbut it doesn't help. @mickl 's answer works perfectly!