Please find below document structure of mongo Db
{ _id : 0,
name : "Employee1",
distributionList :[ { dlname : "ALLEmployee"}, {dlname:"financeall"} ],
csrActivity : [ {activityname : "blooddonation"}, {activityname : "tree plantation"} ]
}
I want list of employee belonging to financeall distribution list and opted to volunteer tree plantation CSR activity.
Expected resultset as below
{ _id : 0,
name : "Employee1",
distributionList :[{dlname:"financeall"} ],
csrActivity : [ {activityname : "tree plantation"} ]
}
But so far able to achieve below output from query
Query :
db.employee.find(
{name : "Employee1"},
{distributionList : {$eleMatch : {dlname : "financeall"}}}
)
Output :
{ _id : 0,
name : "Employee1",
distributionList :[{dlname:"financeall"} ]
}
Using $elemMatch to get the desired output but unable to find how to use it on multiple array within same document. Also tried certain combination but unable to get desired result.
Below query tried but not getting desired output
db.employee.find(
{name : "Employee1"},
{distributionList : {$eleMatch : {dlname : "financeall"}}},
{csrActivity: {$eleMatch : {activityname : "tree plantation"}}}
)
Any help will be highly appreciated
db.employee.find( {name : "Employee1"}, {distributionList : {$elemMatch : {dlname : "financeall"}}, csrActivity: {$elemMatch : {activityname : "tree plantation"}}} )