Mongoose Populate非常棒。但有人可以使用填充来通过另一个不属于_Id的属性来提取文档吗?
示例评论模式:
var CommentSchema = new Schema({
_id:Auto Generated
postId:1,
userId:411,
comment:'This is a comment',
// Would want to fetch Like via the userId not likes _id
likes: {
type:mongoose.Schema.Types.ObjectId, // This will have to change?
ref:'Like'
}
})
和Schema一样:
var LikeSchema = new Schema({
_Id:Auto Generated,
blogId:1,
userId:411,
postId:1,
commentId:1
});
评论模式中的'喜欢' 被设置为userId,在这种情况下为' 411' 。所以我现在假设从上面的Schema中,populate将尝试将CommentSchema中的喜欢与userId 匹配到LikeSchema的_Id。我可以加入这两个by userId?
欢迎任何人的任何想法,只是想扩大我对Mongoose的Populate功能的了解:)