【发布时间】:2014-10-27 09:57:52
【问题描述】:
在我尝试使用 Node 和 Mongoose 编写的 API 中,以下查询:
User.findOne({username: req.params.username}, "-_id -__v")
.populate({path: "songs", select: "-_id -__v"})
.populate({path: "following", select: "-_id username email"})
.exec(function(err, user) {
res.send(user);
});
返回以下 JSON:
{
"email": "alice@alice.org",
"username": "alice",
"following": [
{
"username": "john",
"email": "john@john.org"
}
],
"songs": [
{
"slug": "dvorak-cello-concerto",
"artist": "5403e825cc9c45e9c55c4e7d",
"title": "Cello Concerto"
}
]
}
在歌曲架构中,我将艺术家设置如下:
artist: {type: Schema.Types.ObjectId, ref: 'User'}
还在初始查询中填充的每首歌曲中填充“艺术家”属性的最佳方法是什么,而不仅仅是引用该歌曲所属用户的 _id?
【问题讨论】: