![](/img/trans.png)
[英]How to retreive an object from an array of Objects in mongodb given a objectid
[英]NodeJS and MongoDB - Array of ObjectId how to return Objects
我在集合中有一个ObjectId的引用数组。 我想返回与json响应中的那些ID关联的对象数据。
retrieveFromUser: function( req, res ) {
var user_id = req.params.user_id;
User.findById( user_id, function( err, user ) {
if( err ) {
res.send( 404, "Unable to find user");
} else {
// This returns the array but I want the objects data
return res.json( user.constructions );
}
});
}
我怎样才能做到这一点 ?
非常感谢 !
尝试使用populate()方法
retrieveFromUser: function( req, res ) {
var user_id = req.params.user_id;
User.findOne({ _id: user_id }).populate('constructions').exec(function( err, user ) {
if( err ) {
res.send( 404, "Unable to find user");
} else {
// This returns the array but I want the objects data
return res.json( user.constructions );
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.