Mongoose nested query Mongoose nested query mongoose mongoose

Mongoose nested query


const {userId}=req.params;Contact.findOne({"contacts._id":userId}, {contacts:{$elemMatch:{_id:userId}}).then(data=>res.send(data.contacts[0])).catch(err=>res.send("Not found!"))

This will return only the matched user details based on the userId stored as _id, exactly what you want.