2013-07-10 9 views
1

나는 익스프레스와 몽구스로 피드 리더 앱을 쓰고있다. 내가 3 스키마가 :몽구스 참조 된 문서를 채우는 방법

CategorySchema = new mongoose.Schema({ 
       title:{type:String, unqiue:true, required:true}, 
       created_at:{type:Date, default:Date.now}, 
       order:Number, 
       _feeds:[ 
        {type:mongoose.Schema.Types.ObjectId, ref:"Feed"} 
       ] 
      }); 

FeedSchema = new mongoose.Schema({ 
       xmlurl:{type:String, unique:true, required:true}, 
       title:{type:String, required:true}, 
       original_title:String, 
       link:{type:String, required:true}, 
       favicon:String, 
       date:Date, 
       description:String, 
       _articles:[ 
        {type:mongoose.Schema.Types.ObjectId, ref:'Article'} 
       ], 
       _created_at:{type:Date, default:Date.now}, 
       _category:{type:mongoose.Schema.Types.ObjectId, ref:"Category"} 

      }); 

ArticleSchema = new mongoose.Schema({ 
       title:{type:String, required:true}, 
       description:String, 
       summary:String, 
       meta:mongoose.Schema.Types.Mixed, 
       link:{type:String, required:true}, 
       guid:String, 
       categories:[String], 
       tags:[String], 
       pubDate:{type:Date, default:Date.now}, 
       _feed:{ 
        type:mongoose.Schema.Types.ObjectId, 
        ref:"Feed", 
        required:true 
       }, 
       _favorite:Boolean, 
       _read:Date, 
       _created_at:{type:Date, default:Date.now} 
      }); 

카테고리 것은이 피드와 피드는 기사가있다. 자신의 피드

mongoose.model("Category").find().populate("_feeds").exec(callback); 

내가 범주를 채울 수 있습니다 지금 내가 읽은 자신의 기사와 함께 피드를 채우는 범주에서 싶습니다.

어떻게 할 수 있습니까?

소스 : https://github.com/Mparaiso/FeedPress/blob/master/lib/database.js

감사합니다.

// retrieve all feeds in the list and populate them 
mongoose.model('Feed') 
    .find({ _id : { $in : category._feeds } }) // see text 
    .populate('_articles') 
    .exec(...); 

(나는 처음 $in에 전달 된 배열이 ObjectId 's의 목록을해야한다고 생각하지만, 분명히 그것은 배열을 전달 괜찮습니다 :

답변

0

하나 개의 범주 문서의 경우 그 같은 것을 볼 수 있었다 편집

) 서류 :이 역시 작동 생각 :

mongoose.model('Feed') 
    .populate(category._feeds, { path : '_articles' }) 
    .exec(...); 
관련 문제