MongoDB-Query Optimization
One way to reduce the number of records being filtered further is to include the field grp_id
, in the first $match
operator.
db.member_id_transactions.aggregate([ {$match:{ "id_url.mid": 'xyz12794',"grp_id": 654 } },{$unwind: "$id_url" },{$match: { "id_url.mid": "xyz12794" } } ])
See how the performance is now. Add grp_id
to the index to get better response time.
The above aggregation query though it works, is unnecessary. since you are not altering the structure of the document, and you expect only one element in the array to match the filter condition, you could just use a simple find
and project
.
db.member_id_transactions.find({ "id_url.mid": "xyz12794","grp_id": 654 },{"_id":0,"grp_id":1,"id_url":{$elemMatch:{"mid":"xyz12794"}}, "user_id":1,"mod_on":1,"crtd_on":1,"uploadTp":1, "tp":1,"status":1,"incl":1,"total_cnt":1, "succ_cnt":1,"fail_cnt":1})