2016-03-15 96 views
0

猫鼬填充效果很好。但是,有人可能使用填充来拉取另一个不属于_Id的属性的文档吗?猫鼬填充方法不_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' 
    } 
}) 

和喜欢架构:

var LikeSchema = new Schema({ 
    _Id:Auto Generated, 
    blogId:1, 
    userId:411, 
    postId:1, 
    commentId:1  
}); 

'喜欢'注释模式中被设置为用户id在这种情况下为'411' 。所以我现在假设从上面的Schema中填充将试图匹配,LikeSchema的用户ID为,LikeSchema的_Id。我可以通过userId加入两者吗?

任何人任何想法是值得欢迎的,只是想拓宽我的填充功能方面的知识在猫鼬:)因为(实际上最新)的MongoDB 3.2 $查找流水线方法的版本使用时

+0

简短的回答是否定的。可能的诱惑http://stackoverflow.com/questions/19287142/populate-a-mongoose-model-with-a-field-that-isnt-an-id;看到未被接受的答案。 – JohnnyHK

+0

在发布之前检查了该问题/答案。从2013年起,可能会有事情发展或变化。 – HireLee

回答