【发布时间】:2019-07-07 03:07:06
【问题描述】:
我在我的猫鼬代码中创建了 2 个不同的架构,如下所示。
//News Schema
var newsSchema = mongoose.Schema({
link: { type: String, index: {unique: true, dropDups: true}, required: 'Kindly enter the link of the news' },
description: { type: String, required: 'Kindly enter the description of the news' }
});
//Comment Schema
var commentSchema = mongoose.Schema({
text: { type: String, required: 'Kindly enter the comment text'},
newsId: { type: mongoose.Schema.Types.ObjectId, ref: "News", required: 'Provide the news ID to which this comment belongs' }
},
{
timestamps: true
});
它有新闻和评论模式。每个新闻项目都有多个 cmets,因此我将新闻 ID 作为评论模式的一部分提供。
当我获取新闻项目列表时,我还希望为每个新闻项目获取前 3 个(或更少,如果可用的 cmets 较少)的 cmets。
是否可以填充或聚合或组合它们?或者有没有更好的处理方法?
【问题讨论】:
-
是的,你可以用
aggregate来做 -
@ŞivāSankĂr 您能否提供一些参考文件或示例来实现它?
标签: mongoose mongoose-schema mongoose-populate