singhakash
singhakash

Reputation: 7919

node js mongoose find data from array in collection

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

Upvotes: 1

Views: 631

Answers (3)

chridam
chridam

Reputation: 103365

Use regex filtering on your final result array as follows:

var rgx = new RegExp(name, 'i');
User.find({"ngos.name": rgx})
    .lean()
    .exec(function(err, users) {
        if (err) return next(err);
        var result = users.map(function (n){
            return n.ngos.filter(function(val){return rgx.test(val.name);});
        })  
        console.log(JSON.stringify(result, undefined, 4));
        res.json(result);
    });

Check the demo below.

var cursor = [
	{   
		"ngos": [
			{
				"_id": "564b7527ecc479e4259edff7",
				"name": "Children trust",

			},
			{
				"_id": "564b79e0ecc479e4259edff8",
				"name": "Charity One",
				"location": "Australia"

			}
		]
	},
	{    
		"ngos": [
			{
				"_id": "564e0b3bc8cd4b5420a92510",
				"name": "Charity Two",
				"location": "US"

			}
		]
	}
];
var rgx = new RegExp('Charity', 'i');
var result = cursor.map(function (n){
	return n.ngos.filter(function(val){return rgx.test(val.name);});
})	

pre.innerHTML = "result: " + JSON.stringify(result, null, 4);
<pre id="pre"></pre>

Upvotes: 1

karthick Ramanathan
karthick Ramanathan

Reputation: 910

just try this way in mongoose

you getting res is Array so use "forEach"

User.find({'ngos.name':new RegExp('name', 'i')}, {'ngos':1},function(err,users){
users.forEach( function(user) {
    user.ngos.forEach( function(nom) {
        console.log( nom );
    })
} );

})

just try this way in mongodb

db.user.find({'ngos.name':new RegExp('name', 'i')},{'ngos':1}).forEach( function(user) { 
user.ngos.forEach( function(nom) { 
    print( nom );
    })
} )

I think this help to u !

Upvotes: 0

Ajith Pai
Ajith Pai

Reputation: 34

Hope this helps,

User.find({"ngos.name": new Regex(name, 'i')},{'ngos':1}).exec(function(err,data){
    if (err) throw(err);
    res.json(data);
});

Upvotes: 0

Related Questions