[英]node js mongoose find data from array in collection
I am working with node.js and mongoose I am stuck in a problem. 我正在使用node.js和mongoose,但遇到了问题。 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. 我想找到所有名字像Charity
的ngos
,所以它应该还给我。
{
"_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);
当我返回用户时,它给了我两个用户所有的数据,但是如果我更改了res.json(user);
to res.json(user.ngos);
到res.json(user.ngos);
I am not getting any response. 我没有得到任何回应。
How can I retreive those particular ngos whose name matches? 我该如何检索名称匹配的特定NGO?
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" 你得到res是数组,所以使用“ 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 只是在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 ! 我认为这对您有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.