我的收藏品如下:
socket.io
这里的玩家[]是一个id的数组。
目标是获取此数组的ID,然后为每个Id从另一个集合中获取完整的个人资料信息。
简而言之,这需要加入,但由于加入不存在,我使用异步。
Ofcourse
var followSchema = new Schema({
facebookId: {type: String, required: true},
players : [],
});
我不喜欢上述解决方案,如果我关注200个用户,我将拨打200个mongoose电话。 如果有办法优化此查询?
答案 0 :(得分:0)
正常的方法是populate
http://mongoosejs.com/docs/populate.html
在你的情况下:
FollowModel.findOne({facebookId:req.user.facebookId})
.populate('players')
.exec(function(err, result) {
// now your players array is filled with full player objects
....
}
为此,您需要在架构中添加引用类型:
var followSchema = new Schema({
facebookId: {type: String, required: true},
players : [{type: ObjectId, ref: 'User'}],
});