I am working with node.js and mongoose I am stuck in a problem. My users collection looks like.
{
"_id": ObjectId("564b6deec50de8d827c0a51a"),
"email": "[email protected]",
"ngos": [
{
"_id": ObjectId("564b7527ecc479e4259edff7"),
"name": "Children trust",
},
{
"_id": ObjectId("564b79e0ecc479e4259edff8"),
"name": "Charity Two",
"location": "Australia"
}
]
}
{
"_id": ObjectId("564e0a18c8cd4b5420a9250c"),
"email": "[email protected]",
"ngos": [
{
"_id": ObjectId("564e0b3bc8cd4b5420a92510"),
"name": "Charity Two",
"location": "US"
}
]
}
I want to find all the ngos whose name is like Charity so it should return me.
{
"_id": ObjectId("564e0b3bc8cd4b5420a92510"),
"name": "Charity Two",
"location": "US"
}
{
"_id": ObjectId("564e0b3bc8cd4b5420a92510"),
"name": "Charity Two",
"location": "Australia"
}
I tried
User.find({"ngos.name": new RegExp(name, 'i')}, function(err, user) {
if (err) return next(err);
res.json(user);
});
It gave me both users with all the data as I am returning the user but if I change res.json(user); to res.json(user.ngos); I am not getting any response.
How can I retreive those particular ngos whose name matches?
Thanks