1

I'm writing a feed reader app with express and mongoose. I have 3 schemas :

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}
            });

Categories have feeds and feeds have articles.

i can populate categories with their feeds

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

now i'd like from the Category ,to populate the feeds with their articles that have been read.

How could i do that ?

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

thanks.

4

1 回答 1

0

对于一个类别文档,它可能看起来像这样:

// 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的列表,但显然传递文档数组是可以的)

编辑:我认为这也有效:

mongoose.model('Feed')
  .populate(category._feeds, { path : '_articles' })
  .exec(...);
于 2013-07-13T06:42:11.557 回答