简体   繁体   中英

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": "ak@gmail.com",
    "ngos": [
        {
            "_id": ObjectId("564b7527ecc479e4259edff7"),
            "name": "Children trust",

        },
        {
            "_id": ObjectId("564b79e0ecc479e4259edff8"),
            "name": "Charity Two",
            "location": "Australia"

        }
    ]
}
{
    "_id": ObjectId("564e0a18c8cd4b5420a9250c"),
    "email": "some@gsomel.com",
    "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

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> 

Hope this helps,

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

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 !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM