Mongoose如何填充引用的文档
Mongoose how to populate referenced documents
我正在用express和mongoose编写一个提要阅读器应用程序。我有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
的列表,但显然可以传递一个文档数组)
编辑:我认为这也行:
mongoose.model('Feed')
.populate(category._feeds, { path : '_articles' })
.exec(...);
相关文章:
- '文档'在子窗口中意外地引用了父窗口
- 如何在MongoDB中检索子文档引用
- 在不引用文档的情况下使用AngularJS获取元素
- 是否可以引用猫鼬(MongoDB)上的子文档
- 使用 Mongoose 在所有现有集合中查找引用的文档
- 如何设置javascript中引用的文档的URL
- 未捕获的引用错误:未在Javascript(Chrome控制台)中定义文档
- 如何引用我网站上的文档以独立于网站位置和使用位置
- 如何使用 ExtendScript 从 InDesign 文档的所有脚注引用中删除斜体和粗体样式
- 不确定文档引用器是否正常工作
- 使用_id引用数组对多个相应文档进行高效查询
- 未捕获的引用错误:未定义文档
- 如何填充子“引用”文档猫鼬数组
- mongoDB:在具有父引用的模型树结构中查找缺失的文档
- Javascript - 如何使用哈希获取文档引用
- 当我们尝试使用直接引用调用文档对象方法时,例如:var kk = document.getElementById;kk(
- holder.js未在html文档中引用或加载
- 将嵌套文档集合转换为具有父引用的模型树结构
- 如何在另一个集合中插入具有字段引用文档的文档
- 如何引用文档.在React组件中工作