const pipeline = [ {"$lookup": { from:"finavigatoruserprofiles", localField:"a2", foreignField:"_id", as:"agent"}}, {"$lookup": { from:"finavigatoruserprofiles", localField:"a3", foreignField:"_id", as:"client"}}, {"$lookup": { from:"finavigatorvenues", localField:"a4.a0", foreignField:"_id", as:"venue"}}, {"$lookup": { from:"finavigatormessages", localField:"a5", foreignField:"_id", as:"notes"}}, {"$lookup": { from:"finavigatorleads", localField:"a6", foreignField:"_id", as:"lead"}}, {"$unwind":"$agent"}, {"$unwind":"$client"}, {"$unwind":"$venue"}, {"$unwind":"$lead"}, {"$match":{"venue.a2":"The Nook"}}, {"$group":{ "_id":{ "id":"$_id", "created":"$a0", "pat":"$a4", "venue":"$venue", "agent":"$agent", "client":"$client", "lead":"$lead", "referral":"$a7", "status":{"$arrayElemAt":["$a8",-1]}}, "notes":{"$push":"$notes"} }}, {"$unwind":"$notes"}, {"$project":{ "id":"$_id.id", "created":"$_id.created", "agent":{ "id":"$_id.agent._id", "username":"$_id.agent.a2", "img":"$_id.agent.a11"}, "client":{ "id":"$_id.client._id", "username":"$_id.client.a2", "img":"$_id.client.a11"}, "pat":{ "date":"$_id.pat.a1", "time":"$_id.pat.a2", "venue":{name:"$_id.venue.a2",loc:"$_id.venue.a4.info"}}, "notes":"$notes", "lead":{ "id":"$_id.lead._id", "created":"$_id.lead.a0", "method":"$_id.lead.a2", "campaign":"$_id.lead.a3"}, "referral":"$_id.referral", "status":"$_id.status"}, //"count":{"$sum":1}, } ];