[英]Mongoose how to populate referenced documents
I'm writing a feed reader app with express and mongoose. 我正在编写一个快递和猫鼬的提要阅读器应用程序。 I have 3 schemas :
我有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}
});
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. 现在,我想从Category中,用已阅读的文章填充feed。
How could i do that ? 我该怎么办?
source : https://github.com/Mparaiso/FeedPress/blob/master/lib/database.js 来源: https : //github.com/Mparaiso/FeedPress/blob/master/lib/database.js
thanks. 谢谢。
For one category document, that could look something like this: 对于一个类别文档,可能看起来像这样:
// retrieve all feeds in the list and populate them
mongoose.model('Feed')
.find({ _id : { $in : category._feeds } }) // see text
.populate('_articles')
.exec(...);
(I initially thought that the array passed to $in
should be a list of ObjectId
's, but apparently it's okay to pass an array of documents) (我最初认为传递给
$in
的数组应该是ObjectId
的列表,但是显然可以传递文档数组)
EDIT : I think this works, too: 编辑 :我认为这也有效:
mongoose.model('Feed')
.populate(category._feeds, { path : '_articles' })
.exec(...);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.